Title: Injection of env-entry Example
# 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 ** 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](custom-injection.html)
example for details.
_The source for this example is the "injection-of-env-entry" directory
located in the [openejb-examples.zip](openejb:download.html)
available on the [download page](http://tomee.apache.org/downloads.html)._
# The Code
## Annotated Bean Class
{snippet:id=code|url=openejb3/examples/injection-of-env-entry/src/main/java/org/superbiz/injection/InvoiceBean.java|lang=java}
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
{snippet:id=code|url=openejb3/examples/injection-of-env-entry/src/main/resources/META-INF/ejb-jar.xml|lang=xml}
# Test Case
{snippet:id=code|url=openejb3/examples/injection-of-env-entry/src/test/java/org/superbiz/injection/InvoiceBeanTest.java|lang=java}
# Running it
Running the example is fairly simple. In the "injection-of-env-entry"
directory of the [examples zip](openejb:download.html)
, 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://tomee.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