Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

...

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:

  • DataStore session bean
  • DataReader session bean

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

...

Bean

Wiki Markup
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreLocalDataStoreImpl.java|lang=java}

...

Local business interface

Wiki Markup
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreRemoteDataStoreLocal.java|lang=java}

...

Remote business interface

Wiki Markup
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreImplDataStoreRemote.java|lang=java}

DataReader session bean

Local business interface

Wiki Markup
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataReaderLocal.java|lang=java}

Bean

Wiki Markup
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataReaderImpl.java|lang=java}

...

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

setUp

Wiki Markup
{snippet:id=setup|url=openejb3/examples/injection-of-ejbs/src/test/java/org/superbiz/injection/EjbDependencyTest.java|lang=java}

Test

Wiki Markup
{snippet:id=test|url=openejb3/examples/injection-of-ejbs/src/test/java/org/superbiz/injection/EjbDependencyTest.java|lang=java}

...

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.

No Format
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
[OPENEJB:surefire] Running org.apache.openejb.examplessuperbiz.injection.EjbDependencyTest
log4j:WARN No appenders could be found for logger (OpenEJB).
log4j:WARN Please initialize the log4j system properly.
Apache OpenEJB 3.0-incubating-SNAPSHOT    build: 2007010520080408-1204:4513
http://incubatoropenejb.apache.org/
INFO - openejb
OpenEJB ready.
[OPENEJB:surefire] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 2.006 sec
[OPENEJB:INFO] [jar:jar]
[OPENEJB:INFO] Building jar: openejb3.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/ejb-injection-sample-1.0-SNAPSHOT.jar
[OPENEJB:INFO] [install:install]
[OPENEJB:INFO] Installing openejb3classes
INFO - Configuring app: /Users/dblevins/work/openejb-3.0/examples/injection-of-ejbs/target/ejb-injection-sample-1.0-SNAPSHOT.jar to ...
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] BUILD SUCCESSFUL
[OPENEJB:INFO] ------------------------------------------------------------------------
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