Documentation

EJB 3.0

Deployment

Configuration

Commands

Feeds

 

Overview

The EJB 3.0 spec added Dependency Injection as a main feature. The @Resource annotation can be used to inject several things including EntityManagers, DataSources, Topics, Queues, etc. Most of these are container supplied objects. It is possible, however, to supply your own values to be injected via an <env-entry> in your ejb-jar.xml deployment descriptor. EJB 3.0 supported env-entry types are limited to the following:

  • java.lang.String
  • java.lang.Integer
  • java.lang.Short
  • java.lang.Float
  • java.lang.Double
  • java.lang.Byte
  • java.lang.Character
  • java.lang.Boolean

It should be noted that OpenEJB does not restrict you to just these data types and allows for anything convertible from a String to be injected provided there is a java.beans.PropertyEditor installed to handle the type you want. Additionally, a plain properties file packed in your META-INF directory can be used to supply the injected values instead of using the ejb-jar.xml. See the Custom Injection example for details.

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

The Code

Annotated Bean Class

@Stateful
public class InvoiceBean implements Invoice {

    private int maxLineItems;

    private List<LineItem> items = new ArrayList<LineItem>();

    private int itemCount;

    /**
     * Injects the <b>maxLineItems</b> simple environment entry through bean
     * method.
     *
     * The JavaBeans property name (not the method name) is used as the default
     * JNDI name. By default, the JavaBeans propery 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
     *
     * Refer "EJB Core Contracts and Requirements" specification section 16.2.2.
     *
     * @param maxLineItems
     */
    @Resource
    public void setMaxLineItems(int maxLineItems) {
        this.maxLineItems = maxLineItems;
    }

    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");
        }
    }

    public int getMaxLineItems() {
        return this.maxLineItems;
    }

}

The use of the @Resource annotation isn't limited to setters. For example, this annotation could have been used on the corresponding field like so:

@Resource
private int maxLineItems;

ejb-jar.xml

<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" metadata-complete="false">
  <enterprise-beans>
    <session>
      <ejb-name>InvoiceBean</ejb-name>
      <env-entry>
        <description>The maximum number of line items per invoice.</description>
        <env-entry-name>org.superbiz.injection.InvoiceBean/maxLineItems</env-entry-name>
        <env-entry-type>java.lang.Integer</env-entry-type>
        <env-entry-value>15</env-entry-value>
      </env-entry>
    </session>
  </enterprise-beans>
</ejb-jar>

Test Case

public class InvoiceBeanTest 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 testAddLineItem() throws Exception {
        Invoice order = (Invoice) initialContext.lookup("InvoiceBeanRemote");
        assertNotNull(order);
        LineItem item = new LineItem("ABC-1", "Test Item");

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

    public void testGetMaxLineItems() throws Exception {
        Invoice order = (Invoice) initialContext.lookup("InvoiceBeanRemote");
        assertNotNull(order);

        int maxLineItems = order.getMaxLineItems();

        assertEquals(15, maxLineItems);
    }
}

Running it

Running the example is fairly simple. In the "injection-of-env-entry" 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.PurchaseOrderBeanTest
Apache OpenEJB 3.0    build: 20080408-04:13
http://openejb.apache.org/
INFO - openejb.home = /Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry
INFO - openejb.base = /Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry
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-env-entry/target/classes
INFO - Configuring app: /Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry/target/classes
INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
INFO - Auto-creating a container for bean InvoiceBean: Container(type=STATEFUL, id=Default Stateful Container)
INFO - Loaded Module: /Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry/target/classes
INFO - Assembling app: /Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry/target/classes
INFO - Jndi(name=InvoiceBeanRemote) --> Ejb(deployment-id=InvoiceBean)
INFO - Jndi(name=PurchaseOrderBeanRemote) --> Ejb(deployment-id=PurchaseOrderBean)
INFO - Created Ejb(deployment-id=InvoiceBean, ejb-name=InvoiceBean, container=Default Stateful Container)
INFO - Created Ejb(deployment-id=PurchaseOrderBean, ejb-name=PurchaseOrderBean, container=Default Stateful Container)
INFO - Deployed Application(path=/Users/dblevins/work/openejb-3.0/examples/injection-of-env-entry/target/classes)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.331 sec
Running org.superbiz.injection.InvoiceBeanTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.068 sec

Results :

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

   

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