After reading these,
I'm still confused over the following things, please correct me if i am wrong.
Thanks alot. :)
But what is a CDI bean? A CDI bean is a POJO, plain old java object, that has been automatically instantiated by the CDI container, and is injected into all, and any qualifying injection points in the application. The CDI container initiates the bean discovery process during deployment.
@EJB injects EJBs only, but @Inject can be used to inject POJOs rather than EJBs. However, @Inject requires that your archive be a BDA (contain beans. xml for EE 6, or implicitly in EE 7). @Inject also has additional CDI-specific capabilities (scopes, interceptors, etc.), but those capabilities incur extra overhead.
EJB 3.0 specification provides annotations, which can be applied on fields or setter methods to inject dependencies. EJB Container uses the global JNDI registry to locate the dependency. Following annotations are used in EJB 3.0 for dependency injection. @EJB − used to inject other EJB reference.
CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 and higher. It allows us to manage the lifecycle of stateful components via domain-specific lifecycle contexts and inject components (services) into client objects in a type-safe way.
I would make the following corrections:
All classes within the same archive as the beans.xml is a CDI bean, including EJBs.
Only EJB can be injected using @EJB
(within another EJB or any other EE managed object including CDI beans), while both CDI bean and EJB bean can be injected using @inject (within EJB bean or CDI bean).
A class annotated as @Stateless (for example) that is injected using @Inject is still an EJB bean, and it may also be a CDI bean if in a bean deployment archive; regardless, it will still be managed by EJB container with all the goodness of pooling and transactional.
Notably, a CDI managed bean is anything that can be @Inject
ed into another CDI bean and can itself use @Inject
, which is true for all EJBs, and @EJB
can be used to inject an EJB into any other EE managed bean (EJB, servlet, CDI managed bean, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With