4 Replies Latest reply on Apr 12, 2011 4:17 AM by Luigi Fugaro

    WELD and injecting from external jars

    Luigi Fugaro Newbie

      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:

      public class MyService {

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

      The rest module contains this class:

      public class MyREST {

          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"?>
          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-->
              <description>Object factory for the CDI Bean Manager</description>

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

      <?xml version='1.0' encoding='utf-8'?>

      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!