"No XML" - Dependency Injection (EJB 3) For Absolute Beginners, or Is Possible To Inject With Less Code / XML?

If you already invested the three minutes working through the post "EJB 3 (@Session) For Absolute Beginners - 3 Minutes Bootstrap, 5 Steps", you can skip the requirements section.


  1. Installled JDK 1.5 (better 1.6) 
  2. An IDE of your choice e.g. vi, emacs, netbeans 6.1 (SE or EE), Eclipse Genymede (SE or EE)
  3. @Stateless, @Local, @Entity, @Id Annotations in classpath
  4. An Java EE 5 capable application server of your choice. It will work with Glassfish v1+ (better v2), JBoss 4.2+, WLS 10+ and probably Geronimo (not tried yed)

What is Dependency Injection (DI)?

DI is a fancy term for a simple thing: someone (in this case something -> the EJB 3 container) cares about managing the dependencies for you. You just have to declare the need for it.
EJB 3 is using annotations for this purpose, absolutely NO XML is needed.

Basically: everything which is stored in JNDI can be injected, instead of "looked up".

What is to do:

  1. To inject one Session Bean to another use the @EJB annotation:
    public class BookServiceBean implements BookService {
        private SearchService search;

    Common error: you cannot inject classes in EJB 3.0, but only interfaces. This will change in EJB 3.1...

  2. To inject a DataSource, Queue, ConnectionFactory, Mail, SessionContext etc. you will need the @Resource annotation:
    public class BookServiceBean implements BookService {
        private DataSource ds;

  3. To use the persistence, just use the @PersistenceContext annotation:
public class BookServiceBean implements BookService {
    private EntityManager em;

How it works:

EJB 3 DI operates in "Convention over Configuration" mode. So in general: in case there is only one possibility - it will be injected.
If there are more than one, you will have to configure manifesting your choice...

The easiest possible start:

...is with Netbeans 6.1 (EE) -  download the first choice (139 MB - all included). It will not only install the IDE, but the application server (Reference Implementation: Glassfish RI), sample database etc. Eclipse's Ganymede is good as well, however you will need more projects (one for EJB 3, one for JPA), setup the application server etc. so it takes slightly more time if you are an absolute beginner :-).

Why is it good:

  1. No overhead - the code is as lean as it can be (any suggestions to make it simpler? :-)). The jar contains nothing else, but the interfaces and classes.
  2. It's simple - and sufficient for most use cases.
  3. Is easy to test: the reference can be set in JUnit directly (I will cover this in one of the next posts)
  4. Is configurable - all annotations can be overruled by XML. Even legacy POJOs can be integrated that way. See an extrem example - the deployment of a Swing Table Model as EJB 3 :-).
  5. No vendor lock in: EJB 3 is one and only component model I now, which is vendor neutral, with several available implementations (JBoss, Glassfish, Weblogic, Websphere, openEJB, SAP, Spring Pitchfork, Geronimo + forgotten ones). Google Guice is, however, promising as well. The dependency to EJB 3 spec itself is low - if it does not work for you, you can deploy this "pojos" to something else...
  6. The EJB 3 container is able to monitor (see e.g. Glassfish CallFlow) the whole invocation chain of EJBs. If you are developer: you probably don't care, but your operations :-)


I'm relatively new to EJB3 and have a question regarding DI in the EJB3 world.

From what I can tell there is no way to inject dependencies into JPA entities (via DI Annotations). This seems like a shortcoming when trying to design a system based on DDD (also which I'm new to). For example if you have a domain object that needs access to another service, how would you go about giving that domain object a reference to the service in the JPA workd? Am I missing something?


Posted by Rich Taylor on August 12, 2008 at 10:13 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments