0

Having @Stateless public class EjbService {...}, is@Inject EjbService myService; ok?

Will it really inject SLSB, or it will create new pojo?
Will such inserted myService be transactional?
Will such inserted myService be thread safe?

bfontaine
  • 18,169
  • 13
  • 73
  • 107
bastiat
  • 1,799
  • 2
  • 19
  • 38
  • Welcome to Stack Overflow! I edited your question to make it easier to understand. Good luck! – bfontaine Nov 18 '15 at 16:43
  • 1
    If your container is using a version of CDI that is part of the Java EE 7 specification, then you can use `@inject` for EJB's in your managed, backing, and CDI beans if you prefer. If you are getting errors, it is likely because your application server is not Java EE 7 compliant (which is common; there aren't a lot of Java EE 7 application servers out there). TomEE, for example, implements the Java EE 6 Web profile. – scottb Nov 18 '15 at 17:12

1 Answers1

0

As @scottb pointed out, if it is a certain version of the spec, the EJB would be injected as EJBs are a superset of injectable objects.

Having said that, let us go one by one:

  1. New POJO? If it is not tied to a pre-defined CDI context, then it would be a new POJO. This may also differ by application server.

  2. Transactional? This totally depends on the application server and if the application server scanning recognizes the EJB, then it depends on the type of transactional control configured in your EJB configuration.

  3. Thread-safe? If instantiated as a POJO by the container it will inherit the thread-safe characteristics of the container (for example, servlet container). Again, an application server may scan it as an EJB and support EJB-style multi-threading when injected as a POJO.

In summary, you may be able to inject a SLSB as a POJO, for after all it is a POJO. However, you cannot rely on EJB-specific behaviour when injected in this way unless you have tested everything out on your application server, which may be harder and error-prone than just adopting an application server that supports EJBs.

In short, when you just inject an EJB as a POJO, you are 'off-spec'. That means that the application server is not guaranteed to conform to EJB specifications.

Akber Choudhry
  • 1,755
  • 16
  • 24
  • 1
    To be clear, if the CDI container is integrated with an EJB container, then `@Inject` is required to inject an EJB reference, not a POJO. If the CDI container is not integrated with an EJB container, then it will return POJOs as you say. – Brett Kail Nov 18 '15 at 22:39