2

我试图让 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但似乎没有任何效果。

谁能帮我搞定这个工作?

4

1 回答 1

1

如果您需要在 servlet 中注入,那么您需要更深入的集成,而不仅仅是 servlet 侦听器。我们通过 openwebbeans-tomcat7 插件来提供它。

最简单的方法是使用我们的安装程序,如我们的公告邮件 https://lists.apache.org/thread.html/15b8cbcdbcc24942dae6d277d75363103a9d45d59047fda0e6abcbbe@%3Cannounce.apache.org%3E中所述

在这种情况下,只需从 web.xml 中删除整个 WebBeansConfigurationListener。如果您想集成到“unpimped”的 servlet 容器中,或者如果您在 GAE 中运行等,则可以这样做。在这种情况下,您可以使用 CDI.current() 在 Servlet 中获取 CDI bean。

您也不再需要任何 javassist 依赖项。很久以前,它在 OWB-1.2.xa 中被删除了。

请随时在我们的邮件列表上联系我们,或在 freenode.net 上的 #openwebbeans 上通过 irc 联系我们!

哦,还有一个提示:您可以试用我们的 Apache Meecrowave 容器,它是 OWB + Tomcat9 + CXF + Johnzon - 全部为 9MB。它包括一个 maven 插件、一个 Arquillian 容器等。

于 2017-07-25T12:48:23.210 回答