What is the difference between @Inject and @EJB in the injection of an EJB?

8

I have an EJB that depends on another EJB, so I'm going to use dependency injection to satisfy this dependency. My question is: what is the difference, advantage or disadvantage between @Inject and @EJB.

    
asked by anonymous 29.08.2014 / 16:41

1 answer

9

For Java EE 6 or 7, the recommendation is to always try to use @Inject , @EJB annotations should be used only when a feature with no counterpart in the @Inject annotation is required.

The idea is that with JSRs 299 (CDI) and 300 (DI) the annotation @Inject has become a unified mechanism, available to all layers of the application, replacing previous annotations specific to technologies such as EJB and JSF.

That said, for some cases you end up having to use the root technology annotations.

For the @EJB annotation there are some typical cases of use like:

  • Circular dependencies (injecting an EJB into itself is a popular workaround when there is declarative transactional demarcation to be respected between methods of the same class)
  • Living with nonstandard mappings, remote EJBs, etc. Things in the EJB world were gradually being standardized, those who survived the Java 1.4 era know that each application server had its own way of exposing EJBs, its own conventions, and so on. The EJB annotation has parameters such as beanName , lookup and mappedName to deal with these variations.

Fonts :

29.08.2014 / 18:33