如何正确配置maven tomcat 9以使用cdi

fv2wmkja  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(375)

我想将cdi添加到Tomcat9中,因为这个servlet容器不支持cdi。这个项目的主要思想是在没有spring框架的情况下用java创建一个rest网站,以改进和学习核心机制和javaee。
我回答了这个问题:
https://stackoverflow.com/a/19003725/13817819
我选择不使用tomee,继续使用Tomcat9。
在遵循教程之后,我无法使其按预期工作,因为注入的类仍然为null,在从servlet调用方法时抛出nullexceptionerror。

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

通过调试代码,我看到注入的类为null,如下图所示。
null的调试确认
为了能够将cdi与tomcat一起使用(按照前面的答案),我结束了下一个设置:
pom.xml

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>cca</groupId>
  4. <artifactId>dadas</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>CDI_SPM_Tomcat</name>
  7. <description>dsadas</description>
  8. <properties>
  9. <maven.compiler.source>1.8</maven.compiler.source>
  10. <maven.compiler.target>1.8</maven.compiler.target>
  11. </properties>
  12. <dependencies>
  13. <!-- Servlet -->
  14. <dependency>
  15. <groupId>javax.servlet</groupId>
  16. <artifactId>javax.servlet-api</artifactId>
  17. <version>4.0.0</version>
  18. <scope>provided</scope>
  19. </dependency>
  20. <!-- JSF API -->
  21. <dependency>
  22. <groupId>javax.faces</groupId>
  23. <artifactId>javax.faces-api</artifactId>
  24. <version>2.3</version>
  25. </dependency>
  26. <!-- CDI -->
  27. <dependency>
  28. <groupId>javax.enterprise</groupId>
  29. <artifactId>cdi-api</artifactId>
  30. <version>2.0</version>
  31. </dependency>
  32. <!-- To be able to CDI with Tomcat -->
  33. <dependency>
  34. <groupId>org.jboss.weld.servlet</groupId>
  35. <artifactId>weld-servlet</artifactId>
  36. <version>2.2.9.Final</version>
  37. </dependency>
  38. </dependencies>
  39. </project>

Map需要一个beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  5. version="1.2" bean-discovery-mode="annotated">
  6. <!-- some content -->
  7. </beans>

通过阅读文档,我发现添加这个 <resource-env-ref> 这是需要的。如果使用.jar文件,则添加一个侦听器(注解)。
web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns="http://java.sun.com/xml/ns/javaee"
  5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  7. id="WebApp_ID" version="3.0">
  8. <!-- <listener>
  9. <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  10. </listener> -->
  11. <resource-env-ref>
  12. <resource-env-ref-name>BeanManager</resource-env-ref-name>
  13. <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  14. </resource-env-ref>
  15. </web-app>

为了测试这个想法和知识,我做了一个快速的可重复测试,包括一个类
测试类

  1. @FacesConfig
  2. @RequestScoped
  3. public class TestClass {
  4. public TestClass() {
  5. // TODO Auto-generated constructor stub
  6. }
  7. public String getString() {
  8. return "String";
  9. }
  10. }

以及我尝试注入类的servlet
servlet

  1. @WebServlet("/test")
  2. public class ApiServlet extends HttpServlet {
  3. private static final long serialVersionUID = 1L;
  4. @Inject
  5. private TestClass testClass;
  6. /**
  7. * @see HttpServlet#HttpServlet()
  8. */
  9. public ApiServlet() {
  10. super();
  11. // TODO Auto-generated constructor stub
  12. }
  13. /**
  14. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  15. */
  16. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. // TODO Auto-generated method stub
  18. System.out.println(testClass.getString());
  19. response.getWriter().append("Served at: ").append(request.getContextPath());
  20. }
  21. /**
  22. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  23. */
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. // TODO Auto-generated method stub
  26. doGet(request, response);
  27. }
  28. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题