Overview
This example demonstrates the use of the injection of environment entries using @Resource annotation.
...
$ cd <openejb3_project_dir>/examples/resource-injection
$ mvn clean install
The Code
Injection through field
maxLineItem field in PurchaseOrderBean 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.
@Resource annotation of a field
Code Block |
---|
@Resource int maxLineItems = 10; |
Injection through a setter method
setMaxLineItem method in InvioceBean 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
@Resource annotation of a setter method
Code Block |
---|
@Resource public void setMaxLineItems(int maxLineItems) { this.maxLineItems = maxLineItems; } |
env-entry in ejb-jar.xml
Code Block | ||||
---|---|---|---|---|
| ||||
<env-entry> <description>The maximum number of line items per invoice.</description> <env-entry-name>org.apache.openejb.examples.injection.InvoiceBean/maxLineItems</env-entry-name> <env-entry-type>java.lang.Integer</env-entry-type> <env-entry-value>15</env-entry-value> </env-entry> |
Using @Resource annotated env-entry
Tip | ||
---|---|---|
| ||
maxLineItems variable is injected by the setMaxLine method above. See also how env-entry is defined in ejb-jar.xml |
Code Block |
---|
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"); } } |
JUnit Test
Writing an JUnit test for this example is quite simple. We need just to write a setup method to create and initialize the InitialContext, and then write our test methods.
Test fixture
Code Block |
---|
protected void setUp() throws Exception { Properties properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); properties.setProperty("openejb.deployments.classpath.include", ".*resource-injection.*"); initialContext = new InitialContext(properties); } |
Test methods
Code Block |
---|
public void testAddLineItem() throws Exception { Invoice order = (Invoice) initialContext.lookup("InvoiceBeanBusinessRemote"); assertNotNull(order); LineItem item = new LineItem("ABC-1", "Test Item"); try { order.addLineItem(item); } catch (TooManyItemsException tmie) { fail("Test failed due to: " + tmie.getMessage()); } } |
Running
Running the example is fairly simple. Just execute the following commands:
...