|
Page Edited :
OPENEJB :
Resource Injection
Resource Injection has been edited by David Blevins (Dec 15, 2007). Change summary: Change install to test phase OverviewThis example demonstrates the use of the injection of environment entries using @Resource annotation. "EJB Core Contracts and Requirements" specification section 16.2.2"
PurchaseOrderBean class shows use of @Resource annotation through a bean field. InvoiceBean class 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 containing example and run Maven: $ cd <openejb3_project_dir>/examples/resource-injection $ mvn clean install The CodeInjection through fieldmaxLineItem 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@Resource
int maxLineItems = 10;
@Resource annotation of a setter method@Resource public void setMaxLineItems(int maxLineItems) { this.maxLineItems = maxLineItems; } env-entry in ejb-jar.xml<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
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 TestWriting 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 fixtureprotected 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 methodspublic 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()); } } RunningRunning the example is fairly simple. Just execute the following commands: $ cd <openejb3_project_dir>/examples/resource-injection ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.apache.openejb.examples.injection.InvoiceBeanTest WARN - 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-SNAPSHOT build: 20070120-10:45 http://incubator.apache.org/openejb INFO - openejb.home = /home/raj/projects/openejb3/examples/resource-injection INFO - openejb.base = /home/raj/projects/openejb3/examples/resource-injection WARN - Cannot find the configuration file [OPENEJB:conf/openejb.xml]. Will attempt to create one for the beans deployed. 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. WARN - Auto-creating a container for bean InvoiceBean: Container(type=STATEFUL, id=Default Stateful Container) WARN - Auto-deploying ejb InvoiceBean: EjbDeployment(deployment-id=InvoiceBean, container-id=Default Stateful Container) WARN - Auto-deploying ejb PurchaseOrderBean: EjbDeployment(deployment-id=PurchaseOrderBean, container-id=Default Stateful Container) INFO - Loaded Module: /home/raj/projects/openejb3/examples/resource-injection/target/classes INFO - OpenEJB ready. OpenEJB ready. Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.571 sec Running org.apache.openejb.examples.injection.PurchaseOrderBeanTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 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 |
Unsubscribe or edit your notifications preferences
