Overview

As noted in the Injection of env-entry Example, the EJB 3.0 supported env-entry types are fairly limited. Also the use of several <env-entry> tags in an ejb-jar.xml can get a bit verbose.

OpenEJB does not restrict you to just these data types or require you to use an ejb-jar.xml to declare them.

  • @Resource can be used on any type for which there is java.beans.PropertyEditor
  • You may install your own PropertyEditors and package them with your app.
  • Java Generics are supported (e.g. List<URI> myURIs)
  • You may use a META-INF/env-entries.properties file as an alternative to an ejb-jar.xml

See Built-in Type Converters for a full list of supported env-entry types.

The source for this example is the "custom-injection" directory located in the openejb-examples.zip available on the download page.

The Code

Bean Class

{snippet:id=code|url=openejb3/examples/custom-injection/src/main/java/org/superbiz/enventries/StratocasterImpl.java|lang=java}

The META-INF/env-entries.properties file

{snippet:id=props|url=openejb3/examples/custom-injection/src/main/resources/META-INF/env-entries.properties|lang=none}

The Custom Type and Editor

Support for java.lang.Enum types is already built-in, but we've decided we'd like to allow abbreviated versions of the enum constants to be usable. We do this by creating a custom PropertyEditor for our Pickup enum like so:

{snippet:id=code|url=openejb3/examples/custom-injection/src/main/java/org/superbiz/enventries/PickupEditor.java|lang=java}

We cleverly install this PropertyEditor in a static block in the Pickup class that will be executed should someone actually reference the Pickup type.

{snippet:id=code|url=openejb3/examples/custom-injection/src/main/java/org/superbiz/enventries/Pickup.java|lang=java}

Test Case

{snippet:id=code|url=openejb3/examples/custom-injection/src/test/java/org/superbiz/enventries/StratocasterTest.java|lang=java}

Running it

Running the example is fairly simple. In the "custom-injection" 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.enventries.StratocasterTest Apache OpenEJB 3.1-SNAPSHOT build: 20080409-12:05 http://openejb.apache.org/ INFO - openejb.home = /Users/dblevins/work/openejb3/examples/custom-injection INFO - openejb.base = /Users/dblevins/work/openejb3/examples/custom-injection 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/openejb3/examples/custom-injection/target/classes INFO - Configuring app: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) INFO - Auto-creating a container for bean StratocasterImpl: Container(type=STATELESS, id=Default Stateless Container) INFO - Loaded Module: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes INFO - Assembling app: /Users/dblevins/work/openejb3/examples/custom-injection/target/classes INFO - Jndi(name=StratocasterImplLocal) --> Ejb(deployment-id=StratocasterImpl) INFO - Created Ejb(deployment-id=StratocasterImpl, ejb-name=StratocasterImpl, container=Default Stateless Container) INFO - Deployed Application(path=/Users/dblevins/work/openejb3/examples/custom-injection/target/classes) Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.705 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • No labels