我想将cdi添加到Tomcat9中,因为这个servlet容器不支持cdi。这个项目的主要思想是在没有spring框架的情况下用java创建一个rest网站,以改进和学习核心机制和javaee。
我回答了这个问题:
https://stackoverflow.com/a/19003725/13817819
我选择不使用tomee,继续使用Tomcat9。
在遵循教程之后,我无法使其按预期工作,因为注入的类仍然为null,在从servlet调用方法时抛出nullexceptionerror。
Servlet.service() for servlet [ApiServlet] throws Exception: NullExceptionError
通过调试代码,我看到注入的类为null,如下图所示。
null的调试确认
为了能够将cdi与tomcat一起使用(按照前面的答案),我结束了下一个设置:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cca</groupId>
<artifactId>dadas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CDI_SPM_Tomcat</name>
<description>dsadas</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- JSF API -->
<dependency>
<groupId>javax.faces</groupId>
<artifactId>javax.faces-api</artifactId>
<version>2.3</version>
</dependency>
<!-- CDI -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<!-- To be able to CDI with Tomcat -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.2.9.Final</version>
</dependency>
</dependencies>
</project>
Map需要一个beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.2" bean-discovery-mode="annotated">
<!-- some content -->
</beans>
通过阅读文档,我发现添加这个 <resource-env-ref>
这是需要的。如果使用.jar文件,则添加一个侦听器(注解)。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- <listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener> -->
<resource-env-ref>
<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>
</web-app>
为了测试这个想法和知识,我做了一个快速的可重复测试,包括一个类
测试类
@FacesConfig
@RequestScoped
public class TestClass {
public TestClass() {
// TODO Auto-generated constructor stub
}
public String getString() {
return "String";
}
}
以及我尝试注入类的servlet
servlet
@WebServlet("/test")
public class ApiServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private TestClass testClass;
/**
* @see HttpServlet#HttpServlet()
*/
public ApiServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println(testClass.getString());
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!