1 Reply Latest reply on Oct 11, 2011 9:31 AM by Nikos Paraskevopoulos

    Not @Injecting servlet

    Nikos Paraskevopoulos Novice

      Hello all,


      I am starting this small project using CDI & Seam 3. It is packaged as EAR with 1 WAR module (using GWT, not JSF), 1 EJB module + client JAR, 1 JPA JAR and libraries (details follow). It is deployed in JBoss 6.0. I did upgrade Weld, so it is now at "Specification-Version: 1.1.Beta2", according to the MANIFEST.MF.


      The problem is servlets in the WAR do not get their @Injected dependencies. In detail:


      Sample code



      1) The dummy managed bean & interface:


      public interface Dummy {
           String echo(String msg);
      }
      
      @SessionScoped
      public class DummyImpl implements Dummy, java.io.Serializable {
           private static final long serialVersionUID = 6754401715765586949L;
           
           @Override
           public String echo(String msg) {
                return "echo: " + msg;
           }
      }
      



      2) The servlet (showing @Injection with setter, same result for attribute injection):


      @WebServlet("/foo")
      public class FooServlet extends HttpServlet
      {
           private static final long serialVersionUID = -1347865425552299919L;
           
           private Dummy dummy;
           
           @Override
           protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                resp.setContentType("text/plain");
                if( dummy != null ) {
                     String msg = req.getParameter("msg");
                     resp.getWriter().println(dummy.echo(msg));
                }
                else {
                     resp.getWriter().println("not injected");
                }
           }
           
           @Inject
           public void setDummy(Dummy dummy) {
                this.dummy = dummy;
           }
      }
      
      



      Calling the servlet results in the "not injected" message. Looking up Dummy programmatically does work and logging at debug level shows that it is installed as a bean. In a similar dummy project, servlet injection does work.


      EAR structure



      sports-ear.ear
      +- lib
      |  +- picketlink-idm-api-1.5.0.Alpha02.jar
      |  +- picketlink-idm-common-1.5.0.Alpha02.jar
      |  +- picketlink-idm-core-1.5.0.Alpha02.jar
      |  +- picketlink-idm-spi-1.5.0.Alpha02.jar
      |  +- seam-config-xml-3.0.0.Final.jar
      |  +- seam-persistence-3.0.0.Final.jar
      |  +- seam-security-api-3.0.0.Final.jar
      |  +- seam-security-impl-3.0.0.Final.jar
      |  +- seam-solder-3.0.0.Final.jar
      |  +- sports-model.jar (contains empty META-INF/beans.xml & non-empty META-INF/seam-beans.xml)
      +- META-INF
      |  +- application.xml (specifies JEE version 6)
      +- sports-ejbClient.jar (EJB client interfaces, no beans.xml)
      +- sports-ejb.jar (EJB bean impl. classes, no beans.xml)
      +- sports-war.war
         +- (web stuff)
         +- META-INF
         |  +- MANIFEST.MF (Class-Path: sports-ejbClient.jar)
         +- WEB-INF
            +- web.xml (specifies Servlet version 3.0)
            +- beans.xml (EMPTY)
            +- deploy (GWT stuff)
            +- lib (several JARs needed by the web app, none with beans.xml)
            +- classes (no META-INF directory)
      



      Questions



      a) Injection in servlets is not working for this project, while it is working in other similar ones. Any possible causes/ideas?


      b) Can I debug the injection procedure? Where should I start? (NOTE: I used setter injection + breakpoint; in other projects it does get called, in this project it doesnt)


      c) Any other suggestions?



      Thanks a lot!


      Nikos