0
votes

Does anyone know why it's so difficult to inject an EJB into a Servlet? I'm using JBoss 6.2. My handler variable is null no matter what I try.

@Local
public interface RequestHandlerInterface {
    public String handleRequest(String authKey);    
}

@Singleton
public class RequestHandler implements RequestHandlerInterface {
...
}

public class Test extends HttpServlet {
    @EJB
    private RequestHandlerInterface handler;  //Always null
}

In application context:

<bean id="requestHandler" class="company.application.RequestHandler" />

In web.xml (v3.0):

<servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>company.test.Test</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/Test</url-pattern>
</servlet-mapping>

  <ejb-local-ref>
    <ejb-ref-name>RequestHandler</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local>company.application.RequestHandlerInterface</local>
  </ejb-local-ref>

I've tried so many combinations, with every @EJB(...) option... any idea?
Note: It is all contained in a single WAR.
Note: I've tried private RequestHandler handler; instead of the interface.
Note: I've tried with (name="ejb/RequestHandler") and mappedName, beanName, lookup
Note: I've tried with @Remote and @Local, with ejb-local-ref and ejb-ref

Note:
JNDI bindings for session bean named RequestHandler in deployment unit deployment "company.war" are as follows:

java:global/company/RequestHandler!company.application.RequestHandlerInterface
java:app/company/RequestHandler!company.application.RequestHandlerInterface
java:module/RequestHandler!company.application.RequestHandlerInterface
java:global/company/RequestHandler
java:app/company/RequestHandler
java:module/RequestHandler
2

2 Answers

1
votes

Use local instead of home as below

<ejb-ref>
   <ejb-ref-name>ejb/RequestHandler</ejb-ref-name>
   <ejb-ref-type>Session</ejb-ref-type>
   <local>company.application.RequestHandlerInterface</local>
</ejb-ref>
0
votes

I found the answer here: How to connect HttpServlet with Spring Application Context in web.xml?

Maybe Jay's answer works for EJB-only configurations. But my app is Spring configured, so I'm guessing it must be some issue whereby Spring takes control and disallows injection unless it knows about it.