You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Overview

This example demostrates the use of the injection of environment entries using <b>Resource</b> annotation.

"EJB Core Contracts and Requirements" specification section 16.2.2"

  • "A field 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 entries88 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."

PurchaseOrder bean shows use of @Resource annotation through a bean field.

Invoice bean 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 containting 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

h2 Injection through field

<b>maxLineItem</b> field in <b>PurchaseOrderBean<b> 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.

h2 Injection through a setter method

<b>setMaxLineItem</b> method in <b>Invioce<b> class is annotated with @Resource annotation to inject a simple environment entry. 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
  • No labels