Documentation

EJB 3.0

Deployment

Configuration

Commands

Feeds

 

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

@Stateless
public class DataStoreImpl implements DataStoreLocal, DataStoreRemote{

	public String getData() {
		return "42";
	}

}

Local business interface

@Local
public interface DataStoreLocal {
	
	public String getData();
	
}

Remote business interface

@Remote
public interface DataStoreRemote {
	
	public String getData();
	
}

DataReader session bean

Bean

@Stateless
public class DataReaderImpl implements DataReaderLocal, DataReaderRemote {
	
	@EJB private DataStoreRemote dataStoreRemote;
	@EJB private DataStoreLocal dataStoreLocal;
	
	public String readDataFromLocalStore() {
		return "LOCAL:"+dataStoreLocal.getData();
	}
	
	public String readDataFromRemoteStore() {
		return "REMOTE:"+dataStoreRemote.getData();
	}
}

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

@Local
public interface DataReaderLocal {
	
	public String readDataFromLocalStore();
	public String readDataFromRemoteStore();
}

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

@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

public class EjbDependencyTest extends TestCase {

    private InitialContext initialContext;

    protected void setUp() throws Exception {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

        initialContext = new InitialContext(properties);
    }

    public void test() throws Exception {
        DataReaderLocal dataReader = (DataReaderLocal) initialContext.lookup("DataReaderImplLocal");

        assertNotNull(dataReader);

        assertEquals("LOCAL:42", dataReader.readDataFromLocalStore());
    	assertEquals("REMOTE:42", dataReader.readDataFromRemoteStore());
    }
}

Running

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
http://openejb.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

   

Apache OpenEJB is an project of The Apache Software Foundation (ASF)
Site Powered by Atlassian Confluence .
[ edit ]