@Inject in Tomcat 7
February 10th, 2011Dependency injection is a technique for decoupling the components of large applications. The specification JSR 330 defines a very abstract way of dependency injection in Java and is part of the Java EE 6 platform. Apache OpenWebBeans implements this specification and provides support for the Apache Tomcat 7 servlet container. This short tutorial is basically a summary, update and extension of this one
Preparing Tomcat for OpenWebBeans
First of all, we have to download Tomcat 7 and OpenWebBeans and unzip them somewhere. For integrating OWB into Tomcat we have to copy some of OWB’s JAR files to Tomcat’s lib folder:
- api/geronimo-atinject_1.0_spec-1.0.jar
- api/geronimo-jcdi_1.0_spec-1.0.jar
- spi/openwebbeans-spi-1.0.0.jar
- plugins/openwebbeans-tomcat7-1.0.0.jar
… and you also need to copy the Interceptor API to that folder. The final step is to add the ContextLifecycleListener to the Server node in Tomcat’s conf/server.xml file:
<Listener className="org.apache.webbeans.web.tomcat.ContextLifecycleListener" />That’s it, our Tomcat now supports CDI.
beans.xml
Don’t forget to add an empty beans.xml file as a marker to every project that contributes implementations to be injected. For general projects this file has to be created in the META-INF folder, for web applications it has to be put in WEB-INF.
Maven
Here are the Maven dependencies for a JSF 2 web application using OpenWebBeans:
<dependency> <groupId>org.apache.openwebbeans</groupId> <artifactId>openwebbeans-impl</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.openwebbeans</groupId> <artifactId>openwebbeans-resource</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.openwebbeans</groupId> <artifactId>openwebbeans-jsf</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.openwebbeans</groupId> <artifactId>openwebbeans-web</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.openwebbeans</groupId> <artifactId>openwebbeans-spi</artifactId> <version>1.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-interceptor_1.1_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_3.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jcdi_1.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-atinject_1.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency>
Update (May 2011)
OpenWebBeans 1.1.0 has been released in March 2011. Unfortunately, I couldn’t get it running with a Tomcat 7.0.14. The following exception is thrown when Tomcat initializes OWB’s listener:
java.util.ServiceConfigurationError: org.apache.webbeans.spi.plugins.OpenWebBeansPlugin: Provider org.apache.webbeans.web.tomcat.TomcatWebPlugin could not be instantiated: java.lang.NoClassDefFoundError: org/apache/webbeans/exception/inject/DefinitionException at java.util.ServiceLoader.fail(ServiceLoader.java:207) at java.util.ServiceLoader.access$100(ServiceLoader.java:164) at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:353) at java.util.ServiceLoader$1.next(ServiceLoader.java:421) at org.apache.webbeans.plugins.PluginLoader.startUp(PluginLoader.java:90) at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(AbstractLifeCycle.java:109) at org.apache.webbeans.web.lifecycle.WebContainerLifecycle.startApplication(WebContainerLifecycle.java:77) at org.apache.webbeans.servlet.WebBeansConfigurationListener.contextInitialized(WebBeansConfigurationListener.java:81) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4723) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226) at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.NoClassDefFoundError: org/apache/webbeans/exception/inject/DefinitionException at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:345) ... 13 more Caused by: java.lang.ClassNotFoundException: org.apache.webbeans.exception.inject.DefinitionException at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 16 more


