EJB3 - obtaining bean via injection vs lookup - what are the differences, implications, gotchas?
Asked Answered
C

3

10

There are two ways I know of to obtain an EJB instance:

  • dependency injection in servlets and EJBs via the @EJB annotation
  • JNDI lookup via Context.lookup anywhere

What are the differences, implications and gotchas in using either of these approaches? Are they the same? Is dependency injection faster than lookup? What about transaction handling and object lifecycle management?

Things I'm aware of include:

annotation

  • works with servlets and and EJBs only
  • convenient syntax
  • container independent

lookup

  • can instantiate different implementations of the EJB interface programatically at run time.
  • works from anywhere - e.g. POJOs.
  • depends on naming convention of container
Clisthenes answered 18/1, 2010 at 11:37 Comment(0)
P
7

Both achieve the same result. It's more a matter of coupling. With annotation, you achieve loose coupling and it's easier to mock and test. With direct lookup, you depend on the initial context which may be unconvenient sometimes.

IMHO lookup does not work everywhere. For instance in Glassfish, a lookup on a local EJB from a POJO will work only if has been "imported" previously with @EJBs(...) on one of the session beans that uses the POJO. See this discussion. You need to understand the difference between the local and global JNDI for that.

My advice would be: use annotation as much as possible. If a POJO need a reference to an EJB, pass it as parameter (e.g. in the constructor). That's called dependency inversion and is anyway a good practice.

Perfectible answered 18/1, 2010 at 12:19 Comment(0)
C
2

Lookup depends on presence of JNDI implementation, that is, you have to configure JNDI implementation in order to run unit tests, then annotated fields can be configured manually.

Coats answered 18/1, 2010 at 11:57 Comment(0)
E
0

I think it's kind a hard to mock annotated EJBs. When using lookup you are able to build some switch according to your environment (test -> LoginMockBean, production -> LoginBean).

Eristic answered 27/1, 2011 at 15:5 Comment(1)
That's just wrong. If it's injected by the container, it means you can easier inject it yourself in the tests, e.g MyBean bean = new MyBean(); bean.injectedBean = new Mock(). Hooking in the lookup is more complicated, especially if code depends on new InitialContext(). How do you return a special version of the context for your tests?Perfectible

© 2022 - 2024 — McMap. All rights reserved.