1 Reply Latest reply on Aug 28, 2013 11:45 AM by wdfink

    EJB3.0 : Dependency injection in Servlet problem

    seeaganesh

      Hi,

      I am trying few things in EJB3.0 DI, using Jboss EAP 6. I created an EAR, with a web module+ EJB (a SLSB).

      I am trying to inject EJB in a servlet. The deployment is successful, but when i try to access the servlet I get exception.

       

      i) Bean local interface.

      package com.gj.ejb3;

      import javax.ejb.Local;

      @Local
      public interface HelloWorldIx {
          public String sayHello(String name);

      }

       

      ii) Bean Class

       

      package com.gj.ejb3;

      import javax.ejb.Local;
      import javax.ejb.Stateless;

      /**
      * Session Bean implementation class HelloWorldSessionBean
      */
      @Stateless(name="HelloWorldSessionBean")
      @Local(HelloWorldIx.class)
      public class HelloWorldSessionBean implements HelloWorldIx {

          /**
           * Default constructor.
           */
          public HelloWorldSessionBean() {
              // TODO Auto-generated constructor stub
          }

      /**
           * @see HelloWorldIx#sayHello()
           */
          public String sayHello(String name) {
              System.out.println("came");
         return name.toUpperCase();
          }

      }

       

      iii) Servlet

       

      package com.gj.servlets;

      import javax.ejb.EJB;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import com.gj.ejb3.HelloWorldIx;
      import com.gj.ejb3.SimpleBeanLocal;

      public class LifeCycle extends HttpServlet {

      @EJB(beanName="HelloWorldSessionBean")
          private HelloWorldIx helloWorldIx;

       

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        String name=request.getParameter("name");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        if(helloWorldIx!=null)
        System.out.println(helloWorldIx.sayHello(name));
        else
        System.out.println("Bean is null");
        out.println("<html><head></head><body>service Name " +name+"</body></html>");
      }

      }

       

      iv) Exception: when I access the servlet, the server shows this error.

       

      java.lang.IllegalArgumentException: Can not set com.gj.ejb3.HelloWorldIx field com.gj.servlets.LifeCycle.helloWorldIx to com.gj.ejb3.HelloWorldIx$$$view1

      sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)

      sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java

       

      Am I missing any configuration????

       

       

      Regards

      Ganesh

        • 1. Re: EJB3.0 : Dependency injection in Servlet problem
          wdfink

          What if you use @Stateless and @EJB without specifying the bean-name and remove @Local from the bean (I use the possible minimum of annotations).

          If all is in the same application you might use the bean without interfaces (EJB3.1) and drop the local interface as well if it is not needed from outside the application.