@EJB annotation vs JNDI lookup

dependency-injectionejbejb-3.0jakarta-eejsf-1.2

Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB annotation?

We're using JSF 1.2 with Sun Application Server 9.0_01.

Our team is debating which approach is better when using SLSBs in a Managed Bean.

I've read the following questions, but was wondering if there was a situation where lookup is preferred.

Best Answer

Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB annotation?

There's no situation where it's better--but situations where it's necessary:

  • when the name to lookup is not known at compile time (I would argue that it's bad design, but that's another issue)
  • when annotations are not supported, e.g. in regular non-managed helper classes and few other cases (We could again argue about whether it's good or bad to depends on EJB in these classes).

If the name to look up is constant and injection is possible, prefer @EJB annotations:

  • Make testing easier
  • Less trouble figuring out local / global JNDI names
Related Topic