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"?›
    ‹Resource name="BeanManager"

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

‹?xml version="1.0" encoding="UTF-8"?›
‹beans xmlns=""

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

‹?xml version="1.0" encoding="UTF-8"?›
‹web-app xmlns:xsi=""3.0"›
  ‹display-name›We want weld‹/display-name›

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:

public class MyServlet extends HttpServlet {
    @Inject Injectee i;
    public MyServlet() {
    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:

In doGet()
Injectee says: I have been injected


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.

  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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.