This example shows how to use the @EJB annotation in a bean class to refer to other beans.

This functionality is often referred as dependency injection, and has been introduced in Java EE 5.

In this particular example, we will create two session stateless beans:

The DataReader bean uses the DataStore to retrieve some informations, and we will see how we can, inside the DataReader bean, get a reference to the DataStore bean using the @EJB annotation, thus avoiding the use of the JNDI API.

The source for this example is the "injection-of-ejbs" directory located in the openejb-examples.zip available on the download page.

The Code

In this example we develop two simple session stateless beans (DataReader and DataStore), and show how we can use the @EJB annotation in one of these beans to get the reference to the other session bean

DataStore session bean



Local business interface


Remote business interface


DataReader session bean



Note the usage of the @EJB annotation on the DataStoreRemote and DataStoreLocal fields. This is the minimum required for EJB ref resolution. If you have two beans that implement the same business interfaces, you'll want to the beanName attribute as follows:

@EJB(beanName = "DataStoreImpl") 
private DataStoreRemote dataStoreRemote;

@EJB(beanName = "DataStoreImpl") 
private DataStoreLocal dataStoreLocal;

Local business interface


(The remote business interface is not shown for the sake of brevity).

:title=@EJB annotation} Several components in Java EE can use the @EJB annotation, such as: * Servlets * ServletContextListeners * Servlet Filters * JSF managed beans * EJB interceptors * JAX-WS service endpoints

Writing a unit test for the example

Writing an unit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods



Running the example is fairly simple. In the "injection-of-ejbs" directory of the examples zip , just run:

$ mvn clean install

Which should create output like the following.

 T E S T S
Running org.superbiz.injection.EjbDependencyTest
Apache OpenEJB 3.0    build: 20080408-04:13
INFO - openejb.home =

/Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs INFO - openejb.base = /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) INFO - Configuring Service(id=Default JDK 1.3 ProxyFactory, type=ProxyFactory, provider-id=Default JDK 1.3 ProxyFactory) INFO - Found EjbModule in classpath: /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/classes INFO - Configuring app: /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/classes INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) INFO - Auto-creating a container for bean DataReaderImpl: Container(type=STATELESS, id=Default Stateless Container) INFO - Loaded Module: /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/classes INFO - Assembling app: /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/classes INFO - Jndi(name=DataReaderImplLocal) --> Ejb(deployment-id=DataReaderImpl) INFO - Jndi(name=DataReaderImplRemote) --> Ejb(deployment-id=DataReaderImpl) INFO - Jndi(name=DataStoreImplLocal) --> Ejb(deployment-id=DataStoreImpl) INFO - Jndi(name=DataStoreImplRemote) --> Ejb(deployment-id=DataStoreImpl) INFO - Created Ejb(deployment-id=DataReaderImpl, ejb-name=DataReaderImpl, container=Default Stateless Container) INFO - Created Ejb(deployment-id=DataStoreImpl, ejb-name=DataStoreImpl, container=Default Stateless Container) INFO - Deployed Application(path=/Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/classes) Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.705 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Copyright © 1999-2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0. Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.