Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changes for better reading

...

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

"The EJB 3.0 specification (EJB Core Contracts and Requirements" specification ) section 16.2.2 "reads:

...

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.

...

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

The InvoiceBean class shows the use of method-level @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/injection-of-env-entryImage Modified

To run the example it change your working directory to the directory containing example injection-of-env-entry and run Maven:
(openejb3_project_dir is the top level directory of OpenEJB3 project)the following maven2 commands:

$ cd injection-of-env-entry$ cd <openejb3_project_dir>/examples/resource-injection

$ mvn clean install

The Code

Injection through field (field-level injection)

The maxLineItem field in PurchaseOrderBean class is annotated with @Resource annotation to inject inform the EJB container the place where in the code the injection of a simple environment entry should take place. Default The default value of10 of 10 is assigned. Deployer can modify the values value of the environment entries at deploy deployment time in using deployment descriptor (ejb-jar.xml).

@Resource annotation of a field

Code Block
@Resource
int maxLineItems = 10;

Injection through a setter method (method-level injection)

The setMaxLineItem method in InvioceBean InvoiceBean class is annotated with @Resource annotation to inject a the 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 beOnly setters can be used as a way to inject environment entry values.

You could look up the env-entry using JNDI lookup() method and the following name:

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

The pattern is to combine the fully-qualified class name and the name of a instance field (or a name of the setter method without set prefix and the first letter lowercased).

@Resource annotation of a setter method

...

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

$ cd <openejb3_project_dir>/examples/resource-injectioninjection-of-env-entry
$ mvn clean test

No Format
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.apache.openejbsuperbiz.examples.injection.InvoiceBeanTestPurchaseOrderBeanTest
WARNApache - 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-OpenEJB 3.0.0-SNAPSHOT    build: 2007012020071218-1001:4541
http://incubatoropenejb.apache.org/openejb
INFO - openejb.home = /home/raj/projects/openejb3/examples/resource-injectionc:\oss\openejb3\examples\injection-of-env-entry
INFO - openejb.base = /home/raj/projects/openejb3/examples/resource-injectionc:\oss\openejb3\examples\injection-of-env-entry
WARN - Cannot find the configuration file [OPENEJB:conf/openejb.xml].  Will attempt to create one for the beans deployed.
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: /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.
WARNc:\oss\openejb3\examples\injection-of-env-entry\target\classes
INFO - Configuring app: c:\oss\openejb3\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)
WARN - Auto-deploying ejb InvoiceBean: EjbDeployment(deployment-idINFO - Loaded Module: c:\oss\openejb3\examples\injection-of-env-entry\target\classes
INFO - Assembling app: c:\oss\openejb3\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-id=Default Stateful Container)
WARNINFO - Auto-deploying ejb PurchaseOrderBean: EjbDeploymentCreated Ejb(deployment-id=PurchaseOrderBean, ejb-name=PurchaseOrderBean, container-id=Default Stateful Container)
INFO - Loaded Module: /home/raj/projects/openejb3/examples/resource-injection/target/classesDeployed Application(path=c:\oss\openejb3\examples\injection-of-env-entry\target\classes)
INFO - OpenEJB ready.
OpenEJB ready.
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 32.571859 sec
Running org.apache.openejb.examplessuperbiz.injection.PurchaseOrderBeanTestInvoiceBeanTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037031 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