I have an EJB that depends on another EJB, so I'm going to use dependency injection to satisfy that dependency. My question is: what is the difference, advantage or disadvantage between @Inject and @EJB.
For Java EE 6 or 7, the recommendation is to always try to use
@EJB annotations should only be used when unparalleled functionality in the
@Inject annotation is required.
The idea is that with JSRs 299 (CDI) and 300 (DI) the
@Inject annotation has become a unified mechanism, available to all application layers, replacing previous annotations specific to technologies such as EJB and JSF.
That said, for some cases you end up having to use root technology annotations.
@EJB annotation there are some typical use cases 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)
- Coexistence with nonstandard mappings, remote EJBs, etc. Things in the EJB world were being standardized little by little, whoever survived the Java 1.4 era is well aware that each application server had its own way of exposing EJBs, its own conventions, etc. The EJB annotation relies on parameters like
mappedNameto handle these variations.
- ADAM BIEN'S WEBLOG – @INJECT VS. @EJB
- SOEn – What is the difference between @Inject and @EJB?
- SOEn – Should I use @EJB or @Inject?