0

我想将 CDI 添加到 Tomcat 9,因为这个 servlet 容器不支持 CDI。该项目的主要思想是在没有 Spring 框架的情况下用 Java 创建一个 REST 网站,以改进和学习核心机制和 JavaEE。

我经历了这个答案:

我选择不使用 TomEE并继续使用 Tomcat 9。

在遵循本教程后,我无法使其按预期工作,因为注入的类仍然为空,从 Servlet 调用方法时抛出 NullExceptionError。

Servlet.service() for servlet [ApiServlet] throws Exception: NullExceptionError

通过调试代码,我看到 Injected 类为空,如下图所示。

调试确认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>

映射需要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 文件,则添加一个 Listener(已注释)。

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

小服务程序

@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);
    }

}
4

0 回答 0