Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB bean and CDI bean and Injection

After reading these,

  • Where to use EJB 3.1 and CDI?
  • How do CDI and EJB compare? interact? and some other articles.

I'm still confused over the following things, please correct me if i am wrong.

  1. All classes within the same package as the bean.xml is a CDI bean except for classes annotate as session/message/singleton.
  2. Only EJB can be injected using @EJB(within another EJB), while both CDI bean and EJB bean can be injected using @inject(within EJB bean or CDI bean).
  3. class annotate as @Stateless(for example) that is injected using @Inject is still a EJB bean, not a CDI bean, and will still be managed by EJB container with all the goodness of pooling and transactional.

Thanks alot. :)

like image 238
lai yoke hman Avatar asked Dec 14 '14 17:12

lai yoke hman


People also ask

What is a CDI bean?

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.

What is the difference between @EJB and @inject?

@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.

What is EJB dependency injection?

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.

What is Java CDI?

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.


1 Answers

I would make the following corrections:

  1. All classes within the same archive as the beans.xml is a CDI bean, including EJBs.

  2. 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).

  3. 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 @Injected 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.).

like image 98
Brett Kail Avatar answered Sep 18 '22 01:09

Brett Kail