1

I used Netbeans.

This is what I did.

  1. I used Maven dependency.

    org.jboss.weld weld-osgi-bundle 3.0.0.Alpha8

  2. Now, as stated in their official documentation, I created Web Pages/META-INF/context.xml with this content:

      auth="Container"
    
      type="javax.enterprise.inject.spi.BeanManager"
    
      factory="org.jboss.weld.resources.ManagerObjectFactory"/>
    

mas is the root in case of my project!!

But, it DIFFERS in here, weld documentation only include without other parameters.

and, I also created beans.xml in WEB-INF folder, which looks like this..

<?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"
   bean-discovery-mode="annotated">
    </beans>

But, it doesn't say so.

And, I added in web.xml

<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>

And, to test if this works or not, I created POJO which had setters and getters, and annotated with @Named and @Dependent. It looked sth like this.

@Named
@Dependent
public class Foo{

    String s1="foo", s2="bar";

    //setters and getters for those!!


}

And, craeted servlet through setup in netbeans.

I added sth like this:

@Inject Foo foo;

And, in the method

protected void processRequest(HttpServletRequest request, HttpServletResponse response)



           throws ServletException, IOException {
    //printwriter out configured

        out.append(foo.gets1());




}

The exception is NullPointerException, and it is definitely because of Foo class, that I expected CDI to work, but it doesn't.

So, the question is, how to configure CDI in Tomcat?

Boris the Spider
  • 54,398
  • 6
  • 98
  • 152
Rockink
  • 180
  • 3
  • 17

1 Answers1

0

CDI is not supported by tomcat out of the box, you could use TomEE to resolve this. Alternatively, you could add the weld servlet jar to WEB-INF/lib folder. A detailed process is provided HERE.

Using Maven, add this dependancy:

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>2.2.11.Final</version>
</dependency>
Sionnach733
  • 4,466
  • 3
  • 31
  • 51