4 Replies Latest reply on Jun 17, 2011 8:49 AM by osnetwork

    JBoss AS 6 - EJB and CDI beans not injected

    osnetwork

      Hello everybody,

       

      I'm trying to inject EJBs into Servlets and it is not working.

      I have an EAR folder containing an ejb application packaged as a jar and 2 war files.

      I have problems with the injection of EJBs and beans in both wars. One war is a web application, while the second is a RESTful (RESTeasy) application.

      From one web application I'm trying to inject EJBs into the servlet, in this way:

       

                 @EJB(mappedName = "myService")

                private MyServiceLocal myService;

       

      I have tried with @EJB only, with different names like "EAR_FOLDER/MyServiceImpl/Local", probably most of the jndi names that pop out my mind.

      I'm definitely lost here, I have no clue why the AS does not inject the EJBs into the servlet.

       

      Also, in the REST application I'm trying to use CDI to inject beans and EJBs, also in this case both @Inject and @EJB annotations are doing nothing.

       

      I have no classpath isolation configured, and I have also tried SNAPSHOT instances of JBoss with the same results. Any ideas?

       

      Thanks in advance,

      Luca

        • 1. Re: JBoss AS 6 - EJB and CDI beans not injected
          jaikiran

          If you have a web.xml, what does its xsd declaration look like?

          • 2. Re: JBoss AS 6 - EJB and CDI beans not injected
            osnetwork

            The web.xml is the same for both and it looks like that:

            <web-app version="3.0" 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_3_0.xsd">

             

            I have also tried to add CDI, so I have added the beans.xml files, and in the web application it looks like that:

             

            <beans 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://docs.jboss.org/cdi/beans_1_0.xsd">

            </beans>

            in the REST application is different:

            <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                    xmlns:weld="http://jboss.org/schema/weld/beans"

                    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">

                   

                    <weld:scan>

                            <weld:include name="org.rest.business.**" />

                            <weld:include name="org.rest.service.**" />

                    </weld:scan>

            </beans>

            Note: I have tried also to remove the beans.xml from the web application with no luck. On the REST application, instead I have tried to modify the beans.xml, even removing the weld:scan tags, but still nothing works

             

            Luca

            • 3. Re: JBoss AS 6 - EJB and CDI beans not injected
              jaikiran

              Please post the entire contents of the web.xml and also the code from your servlet.

              • 4. Re: JBoss AS 6 - EJB and CDI beans not injected
                osnetwork

                Here is the web.xml

                <?xml version="1.0" encoding="UTF-8"?>

                <web-app version="3.0" 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_3_0.xsd">

                        <display-name>pn-web</display-name>

                        <session-config>

                                <session-timeout>30</session-timeout>

                        </session-config>

                        <security-role>

                                <role-name>admin</role-name>

                        </security-role>

                        <security-role>

                                <role-name>user</role-name>

                        </security-role>

                        <security-constraint>

                                <web-resource-collection>

                                        <web-resource-name>IndexManagement</web-resource-name>

                                        <url-pattern>/secure/*</url-pattern>

                                </web-resource-collection>

                                <auth-constraint>

                                        <role-name>admin</role-name>

                                </auth-constraint>

                        </security-constraint>

                        <security-constraint>

                                <web-resource-collection>

                                        <web-resource-name>IndexManagement</web-resource-name>

                                        <url-pattern>/report/*</url-pattern>

                                </web-resource-collection>

                                <auth-constraint>

                                        <role-name>user</role-name>

                                        <role-name>admin</role-name>

                                </auth-constraint>

                        </security-constraint>

                        <security-constraint>

                                <web-resource-collection>

                                        <web-resource-name>IndexManagement</web-resource-name>

                                        <url-pattern>/index.jsp</url-pattern>

                                </web-resource-collection>

                                <auth-constraint>

                                        <role-name>admin</role-name>

                                </auth-constraint>

                        </security-constraint>

                        <login-config>

                                <auth-method>FORM</auth-method>

                                <form-login-config>

                                        <form-login-page>/login.jsp</form-login-page>

                                        <form-error-page>/loginfail.jsp</form-error-page>

                                </form-login-config>

                        </login-config>

                        <error-page>

                                <error-code>403</error-code>

                                <location>/accessdenied.jsp</location>

                        </error-page>

                       

                        <servlet>

                                <description></description>

                                <display-name>getTestList.do</display-name>

                                <servlet-name>getTestList.do</servlet-name>

                                <servlet-class>org.web.controller.getTestList</servlet-class>

                        </servlet>

                        <servlet-mapping>

                                <servlet-name>getTestList.do</servlet-name>

                                <url-pattern>/secure/getTestList.do</url-pattern>

                        </servlet-mapping>

                       

                </web-app>

                 

                and here is the getTestList servlet

                public class getTestList extends HttpServlet {

                 

                        @EJB

                        private TestBeanLocal beanService;

                 

                        /**

                         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

                         *      response)

                         */

                        @Override

                        protected void doPost(HttpServletRequest request,

                                        HttpServletResponse response) throws ServletException, IOException {

                 

                                try {

                                        // Paging options

                                        int start = 0;

                                        int pageSize = 10;

                 

                                        String begin = request.getParameter("start");

                                        String limit = request.getParameter("limit");

                 

                                        if (begin != null && begin.length() > 0) {

                                                start = Integer.parseInt(begin);

                                        }

                 

                                        if (limit != null) {

                                                pageSize = Integer.parseInt(limit);

                                        }

                 

                                        PagedListDTO complete = beanService.getOpenList(start,

                                                        pageSize);

                 

                 

                                        JSONArray jsonArray = JSONOutput.createDataArray(complete

                                                        .getEntries());

                 

                                        response.setContentType("text/html; charset=UTF-8");

                 

                                        String output = JSONOutput.createPagedOuput(jsonArray,

                                                        complete.getTotalCount());

                                        log.debug(output);

                                        response.getWriter().write(output);

                                } catch (Exception e) {

                                        errorJsonResponse(response, e);

                                }

                        }

                }

                and the beanService is null.