我试图让 CDI 在 tomcat 9.x 中工作。我按照以下链接进行操作,但 openwebbeans 容器仍然没有将资源注入 servlet
https://devlearnings.wordpress.com/2011/05/15/apache-openwebbeans-cdi-from-standalone-to-webapp/ https://dzone.com/articles/using-apache-openwebbeans http://openwebbeans .apache.org/owbsetup_ee.html
下面是我的servlet
package com.openwebbeans;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SampleController extends HttpServlet{
private static final long serialVersionUID = 1L;
@Inject
private SampleService service;
public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
response.getWriter().print(service);
}
}
下面是web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>OpenWebBeans</display-name>
<listener>
<listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>
<servlet>
<servlet-name>sample</servlet-name>
<servlet-class>com.openwebbeans.SampleController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sample</servlet-name>
<url-pattern>/sample</url-pattern>
</servlet-mapping>
</web-app>
在 server.xml 中添加了以下行
<Listener className="org.apache.webbeans.web.tomcat7.ContextLifecycleListener" />
下面是我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.openwebbeans</groupId>
<artifactId>openwebbeans-beginner</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>openwebbeans-beginner</name>
<url>http://maven.apache.org</url>
<properties>
<owb.version>2.0.0</owb.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.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>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_2.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-interceptor_1.2_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-annotation_1.3_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-impl</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-spi</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-web</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.openwebbeans</groupId>
<artifactId>openwebbeans-tomcat7</artifactId>
<version>${owb.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>openwebbeans-beginner</finalName>
</build>
</project>
在tomcat lib下添加了以下jar
- geronimo-annotation_1.3_spec-1.0
- geronimo-atinject_1.0_spec-1.0
- geronimo-interceptor_1.2_spec-1.0
- geronimo-jcdi_2.0_spec-1.0
- openwebbeans-el22-2.0.0
- openwebbeans-impl-2.0.0
- openwebbeans-spi-2.0.0
- openwebbeans-tomcat7-2.0.0
- openwebbeans-web-2.0.0
- xbean-asm5-shaded-4.5
- xbean-finder-shaded-4.5
- javassist-3.21.0-GA
以下是部署我的战争后的服务器日志。从日志中可以清楚地看到打开的 web bean 容器已经启动
2017 年 7 月 20 日 10:06:08.315 信息 [http-nio-8080-exec-5] org.apache.catalina.startup.HostConfig.deployWAR 部署 Web 应用程序存档 [D:\krishna\apache-tomcat-9.0.0 .M22\webapps\openwebbeans-beginner.war] 20-Jul-2017 10:06:08.904 信息 [http-nio-8080-exec-5] org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication OpenWebBeans 容器正在启动.. . 20-Jul-2017 10:06:09.229 INFO [http-nio-8080-exec-5] org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication OpenWebBeans Container 已启动,耗时 [325] 毫秒。2017 年 7 月 20 日 10:06:09.235 信息 [http-nio-8080-exec-5] org.apache.catalina.startup.HostConfig.deployWAR 部署 Web 应用程序存档 [D:\krishna\apache-tomcat-9.0. 0.M22\webapps\openwebbeans-beginner.war] 已在 [920] 毫秒内完成
此外,我在 META-INF/openwebbeans 下创建了 openwebbeans.properties 并添加了org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.lifecycle.StandaloneLifeCycle。但它仍然不起作用
我也尝试了org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.web.lifecycle.WebContainerLifecycle但似乎没有任何效果。
谁能帮我搞定这个工作?