4 Replies Latest reply on Apr 12, 2011 4:17 AM by l.fugaro

    WELD and injecting from external jars

    l.fugaro

      Hi to all,
      I'm new here...
      I got a little problem.


      I have a maven project with three sub-modules: web, rest and service.
      The service module contains this class:


      @Dependent
      @Named
      public class MyService {

          public List getList() {
              return new ArrayList();
          }
      }


      The rest module contains this class:


      @Path("/users")
      public class MyREST {

          @Inject
          private MyService myService;

          public Response findAll(@Context HttpServletRequest request, @Context HttpServletResponse response) {
              Response res;
              List list = myService.getList();
              res = Response.ok(list).build();
              return res;
          }

      }


      Guess what?
      Yes, exactly, myService is null and I get a wonderful NullPointerException!


      This means that running the web application I can reach my rest-resource (thus RESTeasy is well configured) but not able to inject my service.


      1. I did try adding @RequestScoped to MyREST, leaving those two classes in their modules... didn't work!!!
      2. I did try moving MyService class only in the rest module... didn't work!!!
      3. I did try moving MyService class only in the web module... HEY... it DID work!!!


      The third try tells me that WELD is well configured... right?


      Isn't it weird, is it?


      This is my env:
      - jdk 1.6
      - maven 2.2.1
      - tomcat 6.0.26
      - weld 1.1.0.Final
      - RESTeasy 2.1.0.GA
      - weld-tomcat-support-1.0.1-Final (just copied into tomcat's lib)


      my web.xml (WEB-INF/):




      <?xml version="1.0" encoding="UTF-8"?>
      <web-app
          version="2.5"
          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/web-app_2_5.xsd">
      
          <!--<display-name>RESTful Web Services Application</display-name>-->
          <!-- Set this if you want Resteasy to scan for JAX-RS classes-->
          <context-param>
              <param-name>resteasy.scan</param-name>
              <param-value>true</param-value>
          </context-param>
          <context-param>
              <param-name>resteasy.servlet.mapping.prefix</param-name>
              <param-value>/rest</param-value>
          </context-param>
          <context-param>
              <param-name>resteasy.injector.factory</param-name>
              <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
          </context-param>
      
          <listener>
              <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
          </listener>
          <listener>
              <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
          </listener>
      
          <servlet>
              <servlet-name>Resteasy</servlet-name>
              <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
          </servlet>
          <servlet-mapping>
              <servlet-name>Resteasy</servlet-name>
              <url-pattern>/rest/*</url-pattern>
          </servlet-mapping>
      
      
          <resource-env-ref>
              <description>Object factory for the CDI Bean Manager</description>
              <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>
      
          <session-config>
              <session-timeout>30</session-timeout>
          </session-config>
          
      </web-app>





      and my context.xml (META-INF/):




      <?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"
                  />
      </Context>





      and my beans.xml (WEB-INF/)
      - empty -
      it is just used to tell the CDI thing to tomcat I guess... I read this some where, without this empty file my 3rd try would not work!


      So, what I want?


      I want to be able to inject MyService into MyREST, having MyService placed into my service module.


      It's like WELD can load/inject things outside the WEB-INF/classes/... all my modules are in WEB-INF/lib/ as excepted!


      Thanks in advance and please ask if you need more details!


      Regards,
      Luigi