Apache OpenWebBeans(CDI) + Servlet, injection not working

1.4k views Asked by At

I am trying to get CDI working in tomcat 9.x. I followed the following links but still openwebbeans container did not inject the resource into the 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

Below is my 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);
    }
}

Below is the 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>

Added the below line in server.xml

<Listener className="org.apache.webbeans.web.tomcat7.ContextLifecycleListener" />

Below is my 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>

Added the below jars under tomcat lib

  • 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

Below are the server logs after deploying my war. It is clear from the logs that open web beans container has started

20-Jul-2017 10:06:08.315 INFO [http-nio-8080-exec-5] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [D:\krishna\apache-tomcat-9.0.0.M22\webapps\openwebbeans-beginner.war] 20-Jul-2017 10:06:08.904 INFO [http-nio-8080-exec-5] org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication OpenWebBeans Container is starting... 20-Jul-2017 10:06:09.229 INFO [http-nio-8080-exec-5] org.apache.webbeans.lifecycle.AbstractLifeCycle.bootstrapApplication OpenWebBeans Container has started, it took [325] ms. 20-Jul-2017 10:06:09.235 INFO [http-nio-8080-exec-5] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [D:\krishna\apache-tomcat-9.0.0.M22\webapps\openwebbeans-beginner.war] has finished in [920] ms

Additionally i created openwebbeans.properties under META-INF/openwebbeans and added org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.lifecycle.StandaloneLifeCycle to it. But it still does not work

I also tried with org.apache.webbeans.spi.ContainerLifecycle=org.apache.webbeans.web.lifecycle.WebContainerLifecycle but nothing seems to work.

Can anyone please help me get this working?

1

There are 1 answers

0
struberg On

If you need injection in servlets then you need a deeper integration than just the servlet listener. We provide this with the openwebbeans-tomcat7 plugin.

The easiest way is to use our installer as explained in our announce mail https://lists.apache.org/thread.html/15b8cbcdbcc24942dae6d277d75363103a9d45d59047fda0e6abcbbe@%3Cannounce.apache.org%3E

In that case just remove the whole WebBeansConfigurationListener from your web.xml. This is intended if you like to integrate in 'unpimped' servlet containers or if you run in GAE, etc. In which case you can work around with using CDI.current() to get your CDI bean in a Servlet.

You also don't need any javassist dependency anymore. That got removed in OWB-1.2.x a long time ago.

Feel free to ping us on our mailing lists or irc at #openwebbeans on freenode.net!

Oh and another tip: you could try out our Apache Meecrowave container, which is OWB + Tomcat9 + CXF + Johnzon - all in 9MB. It includes a maven-plugin, an Arquillian container, etc.