如何解决如何正确配置 Maven Tomcat 9 以使用 CDI
我想将 CDI 添加到 Tomcat 9,因为这个 servlet 容器不支持 CDI。该项目的主要思想是在没有 Spring Framework 的情况下用 Java 创建一个 REST 网站,以改进和学习核心机制和 JavaEE。
我经历了这个答案:
和我选择不使用 TomEE 并继续使用 Tomcat 9。
在按照教程进行操作后,我无法使其按预期工作,因为注入的类仍然为 null,在从 Servlet 调用方法时抛出 NullExceptionError。
Servlet.service() for servlet [ApiServlet] throws Exception: NullExceptionError
通过调试代码,我看到 Injected 类为 null,如下图所示。
为了能够在 tomcat 中使用 CDI(按照上一个答案),我以下一个设置结束:
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>
映射需要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,IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。