What is the difference between @Inject and @EJB when injecting an EJB?

Asked

Viewed 5,156 times

9

I have an EJB that relies 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.

  • Very good @Wakim. Do you think it would be cool to see the differences and put here to the Brazilian public?

  • Is this question about a specific programming language? If so, add the language tag.

1 answer

9


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

The idea is that with Jsrs 299 (CDI) and 300 (DI) the annotation @Inject became a unified mechanism, available for 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 the annotations of the root technology.

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

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

Sources:

  • 1

    +1, Just to complement: comparing on an application server, there is not much difference compared to performance that justifies that CDI Beans are "lighter" than Ejbs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.