Overview

General

Configuration

Servers

Integrations

Community

Feeds

 

Overview

This example demonstrates the use of the injection of environment entries using @Resource annotation.

"EJB Core Contracts and Requirements" specification section 16.2.2"

  • "A ï¬?eld or method of a bean class may be annotated to request that an entry from the bean's environment
    be injected. Any of the types of resources or other environment entries described in this chapter may
    be injected. Injection may also be requested using entries in the deployment descriptor corresponding to
    each of these resource types."
  • "Environment entries may also be injected into the bean through bean methods that follow the
    naming conventions for JavaBeans properties. The annotation is applied to the set method for
    the property, which is the method that is called to inject the environment entry. The JavaBeans
    property name (not the method name) is used as the default JNDI name."

PurchaseOrderBean class shows use of @Resource annotation through a bean field.

InvoiceBean class shows the use of @Resource annotation through a setter method.

The source for this example can be checked out from svn:

$ svn co http://svn.apache.org/repos/asf/incubator/openejb/trunk/openejb3/examples/resource-injection/

To run the example change to the directory containing example and run Maven:
(openejb3_project_dir is the top level directory of OpenEJB3 project)

$ cd <openejb3_project_dir>/examples/resource-injection

$ mvn clean install

The Code

Injection through field

maxLineItem field in PurchaseOrderBean class is annotated with @Resource annotation to inject a simple environment entry. Default value of10 is assigned. Deployer can modify the values of the environment entries at deploy time in deployment descriptor.

@Resource annotation of a field

@Resource
int maxLineItems = 10;

Injection through a setter method

setMaxLineItem method in InvioceBean class is annotated with @Resource annotation to inject a simple environment entry. By default, the JavaBeans property name is combined with the name of the class in which the annotation is used and is used directly as the name in the bean's naming context. JNDI name for this entry would be:

  • java:comp/env/org.apache.openejb.examples.resource.InvoiceBean/maxLineItems

@Resource annotation of a setter method

@Resource
public void setMaxLineItems(int maxLineItems) {
    this.maxLineItems = maxLineItems;
}

env-entry in ejb-jar.xml

<env-entry>
     <description>The maximum number of line items per invoice.</description>
     <env-entry-name>org.apache.openejb.examples.injection.InvoiceBean/maxLineItems</env-entry-name>
     <env-entry-type>java.lang.Integer</env-entry-type>
     <env-entry-value>15</env-entry-value>
</env-entry>

Using @Resource annotated env-entry

Using @Resource annotated env-entry

maxLineItems variable is injected by the setMaxLine method above. See also how env-entry is defined in ejb-jar.xml

public void addLineItem(LineItem item) throws TooManyItemsException {
   if (item == null) {
      throw new IllegalArgumentException("Line item must not be null");
   }

   if (itemCount <= maxLineItems) {
      items.add(item);
      itemCount++;
   } else {
      throw new TooManyItemsException("Number of items exceeded the maximum limit");
   }
}

JUnit Test

Writing an JUnit 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.

Test fixture

protected void setUp() throws Exception {
    Properties properties = new Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    properties.setProperty("openejb.deployments.classpath.include", ".*resource-injection.*");
    initialContext = new InitialContext(properties);
}

Test methods

public void testAddLineItem() throws Exception {
    Invoice order = (Invoice) initialContext.lookup("InvoiceBeanBusinessRemote");
    assertNotNull(order);
    LineItem item = new LineItem("ABC-1", "Test Item");

    try {
        order.addLineItem(item);
    } catch (TooManyItemsException tmie) {
        fail("Test failed due to: " + tmie.getMessage());
    }
}

Running

Running the example is fairly simple. Just execute the following commands:

$ cd <openejb3_project_dir>/examples/resource-injection
$ mvn clean test

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.apache.openejb.examples.injection.InvoiceBeanTest
WARN - Unable use logging config as there are 3 file references containing directories which have not been created.  See the list below.
WARN - [0] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/transaction.log
WARN - [1] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/openejb.log
WARN - [2] /home/raj/projects/openejb3/examples/resource-injection/conf/logs/server.log
Apache OpenEJB 3.0-incubating-SNAPSHOT    build: 20070120-10:45
http://incubator.apache.org/openejb
INFO - openejb.home = /home/raj/projects/openejb3/examples/resource-injection
INFO - openejb.base = /home/raj/projects/openejb3/examples/resource-injection
WARN - Cannot find the configuration file [OPENEJB:conf/openejb.xml].  Will attempt to create one for the beans deployed.
INFO - Found EjbModule in classpath: /home/raj/projects/openejb3/examples/resource-injection/target/classes
WARN - Cannot find the META-INF/openejb-jar.xml in /home/raj/projects/openejb3/examples/resource-injection/target/classes.
WARN - Auto-creating a container for bean InvoiceBean: Container(type=STATEFUL, id=Default Stateful Container)
WARN - Auto-deploying ejb InvoiceBean: EjbDeployment(deployment-id=InvoiceBean, container-id=Default Stateful Container)
WARN - Auto-deploying ejb PurchaseOrderBean: EjbDeployment(deployment-id=PurchaseOrderBean, container-id=Default Stateful Container)
INFO - Loaded Module: /home/raj/projects/openejb3/examples/resource-injection/target/classes
INFO - OpenEJB ready.
OpenEJB ready.
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.571 sec
Running org.apache.openejb.examples.injection.PurchaseOrderBeanTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec

Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] BUILD SUCCESSFUL
[OPENEJB:INFO] ------------------------------------------------------------------------
[OPENEJB:INFO] Total time: 16 seconds

   

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