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
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 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;
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://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
All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.