Getting Weld CDI into Tomcat 7

Once you know it, it’s easy. As usual… So here a short write-up on how to include Weld (1.1.8) into a simple Tomcat 7 Servlet container (obviously without all the Java EE 6 tralala). It’s a result of Googling around, trial & error and RTFM, so no credits to me for this one :)

* Create a new web project in Eclipse.

* Download WELD and include the following files in the ‘lib’ folderweld-api.jar, weld-core.jar, weld-servlet.jar, weld-spi.jar. Do NOT add weld-se-*.jar files. They are not needed in our case and will conflict with the environment provided by Tomcat.

* In WebContent/META-INF, add the context.xml file with the following content:

‹?xml version="1.0" encoding="UTF-8"?›
‹Context›
    ‹Resource name="BeanManager"
              auth="Container"
              type="javax.enterprise.inject.spi.BeanManager"
              factory="org.jboss.weld.resources.ManagerObjectFactory"/›
    ‹WatchedResource›WEB-INF/web.xml‹/WatchedResource›
    ‹WatchedResource›META-INF/context.xml‹/WatchedResource›
‹/Context›

* In WebContent/WEB-INF, add the famous beans.xml file:

‹?xml version="1.0" encoding="UTF-8"?›
‹beans 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/beans_1_0.xsd"›
‹/beans›

* Edit web.xml in WebContent/WEB-INF and make it looking like this:

‹?xml version="1.0" encoding="UTF-8"?›
‹web-app xmlns:xsi="http://www.w3.org/2001....rsion="3.0"›
  ‹display-name›We want weld‹/display-name›
  ‹welcome-file-list›
    ‹welcome-file›index.jsp‹/welcome-file›
  ‹/welcome-file-list›
  ‹listener›
    ‹listener-class›
       org.jboss.weld.environment.servlet.Listener
    ‹/listener-class›
  ‹/listener›
  ‹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›
‹/web-app>

The Weld Listener boots the framework up.

* To test all this, create on simple POJO, object of the injection. Note that we don’t need a scope or a managed bean annotation.

public class Injectee {
    public String sayHi() {
        return ("I have been injected");
    }
}

and since we can only inject in managed classes (i.e. classes “controlled”/known by the CDI framework, a test Servlet:

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    @Inject Injectee i;
    public MyServlet() {
        super();
    }
    protected void doGet(HttpServletRequest rq,HttpServletResponse rp)
                                 throws ServletException, IOException {
        System.out.println("In doGet()");
        System.out.println("Injectee says: "+i.sayHi());
    }
}

At Tomcat start-up, we should see the following lines in the log file. Be aware about the second one :)

INFO: WELD-000900 1.1.8 (Final)
INFO: Tomcat 7 detected, CDI injection will be available in
       Servlets and Filters. Injection into Listeners is not supported

Calling the Servlet shows us what we hope to see:

http://localhost:8080/Probe/MyServlet
../..
In doGet()
Injectee says: I have been injected

Eureka!

More info in the Weld documentation, always useful :)   [Click Me]

(Note: the ‹ and › used above are not the correct < and > characters, so be careful when copying the xml files)

6 thoughts on “Getting Weld CDI into Tomcat 7

  1. Nice job. I did what you said, but in Netbeans 7.1. Big help as we know that anyway we can use Tomcat is good in the long run.

  2. thanks for this post. We have to include jboss-interceptor-core and jboss-interceptor-spi jars too

  3. Thanks for your instructions, but somehow I got this Error:
    WELD-001408 Unsatisfied dependencies for type [Injectee] with qualifiers [@Default] at injection point [[field] @Inject citychallenge.servlets.TestServlet.i]

    Any idea what the cause might be? I’m using tomcat 7.0.39 and only the weld-servlet-1.1.10.Final.jar, as recommended on the weld page.
    Thanks

  4. @Feliciano I experienced the same unresolved dependency issue and resolved it by moving beans.xml to …/classes/META-INF/

Leave a Reply

Your email address will not be published. Required fields are marked *