blob: 8e5ad9fe992c1716c519aba6f01e6e04631543e5 [file] [log] [blame]
Title: Injection of other EJBs Example
<a name="InjectionofotherEJBsExample-Overview"></a>
# 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](openejb:download.html)
available on the [download page](http://tomee.apache.org/downloads.html)._
<a name="InjectionofotherEJBsExample-TheCode"></a>
# 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
<a name="InjectionofotherEJBsExample-DataStoresessionbean"></a>
## DataStore session bean
<a name="InjectionofotherEJBsExample-Bean"></a>
### Bean
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreImpl.java|lang=java}
<a name="InjectionofotherEJBsExample-Localbusinessinterface"></a>
### Local business interface
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreLocal.java|lang=java}
<a name="InjectionofotherEJBsExample-Remotebusinessinterface"></a>
### Remote business interface
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataStoreRemote.java|lang=java}
<a name="InjectionofotherEJBsExample-DataReadersessionbean"></a>
## DataReader session bean
<a name="InjectionofotherEJBsExample-Bean"></a>
### Bean
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataReaderImpl.java|lang=java}
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;
<a name="InjectionofotherEJBsExample-Localbusinessinterface"></a>
### Local business interface
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/main/java/org/superbiz/injection/DataReaderLocal.java|lang=java}
(The remote business interface is not shown for the sake of brevity).
{tip: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
{tip}
<a name="InjectionofotherEJBsExample-Writingaunittestfortheexample"></a>
# 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
{snippet:id=code|url=openejb3/examples/injection-of-ejbs/src/test/java/org/superbiz/injection/EjbDependencyTest.java|lang=java}
<a name="InjectionofotherEJBsExample-Running"></a>
# Running
Running the example is fairly simple. In the "injection-of-ejbs" directory
of the [examples zip](openejb:download.html)
, 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
http://tomee.apache.org/
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