mardi 5 mai 2015

How to configure Weld in Tomcat for CDI?

I used Netbeans.

This is what I did.

  1. I used Maven dependency.

    <dependency>
            <groupId>org.jboss.weld</groupId>
            <artifactId>weld-osgi-bundle</artifactId>
            <version>3.0.0.Alpha8</version>
    </dependency>
    
  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://ift.tt/19L2NlC"
   xmlns:xsi="http://ift.tt/ra1lAU"
   xsi:schemaLocation="http://ift.tt/19L2NlC    http://ift.tt/18tV3H8"
   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?

Aucun commentaire:

Enregistrer un commentaire