上篇写了weld在tomcat中的集成后,发现apache openwebbeans也是可以整合的,自己太粗心,以前用tomEE的时候忘记了这个openwebbeans是其组成部分之一。本篇简单说一下openwebbeans作为CDI实现整合到tomcat中的方法。
其实大部分的整合方式都差不多,细节有变化而已。
依旧是Maven工程,增加依赖项:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-atinject_1.0_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.1_spec</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.2_spec</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>${owb.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${owb.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-web</artifactId>
<version>${owb.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-tomcat7</artifactId>
<version>${owb.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
web.xml中增加配置(包括openwebbeans监听器,BeanManager资源和数据源资源):
<listener>
<listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>
<resource-env-ref>
<description>Object factory for the CDI Bean Manager</description>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>
javax.enterprise.inject.spi.BeanManager
</resource-env-ref-type>
</resource-env-ref>
<resource-ref>
<description>MySQL DBCP</description>
<res-ref-name>jdbc/axon</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
在webroot/META-INF/context.xml中增加配置(BeanManager的JNDI和数据源):
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.apache.webbeans.container.ManagerObjectFactory" />
<Resource name="jdbc/axon" auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdel="10" maxWait="1000" username="root"
password="kknd1234" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/axon?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8">
</Resource>
然后再按照上一篇中的配置,增加struts2的配置即可。JPA也不再重复了,和上一篇的相同。
这样就可以使用了,但是有一个问题我还没解决,就是增加了openwebbeans-tomcat7这个依赖以后,仍然不能按照官方说的可在servlet中进行注入。目前只能在strtus2中注入。有明白的请告诉我一下。(https://dzone.com/articles/using-apache-openwebbeans)