0

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
djb
  • 1,635
  • 3
  • 26
  • 49
  • Possible duplicate of [How to connect HttpServlet with Spring Application Context in web.xml?](https://stackoverflow.com/questions/1866953/how-to-connect-httpservlet-with-spring-application-context-in-web-xml) – Dave Jarvis Jun 01 '17 at 20:00

2 Answers2

1

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>
Jay
  • 9,189
  • 12
  • 56
  • 96
  • (Note, I got rid of "ejb/" to simplify my example) – djb Mar 19 '14 at 09:54
  • Try to remove all the contents and check again. – Jay Mar 19 '14 at 11:53
  • No change. "Injected" RequestHandler still null. – djb Mar 20 '14 at 13:46
  • It worked perfectly fine for me on JBoss EAP 6.1, but I tried on Stateless instead of Singleton EJB. I didn't even defined any – Jay Mar 20 '14 at 14:20
  • Hmm, it may be a scope issue, since Servlets are session scoped, and singletons are application scoped. I ended up setting up a queue between my Servlet and EJB, which is pretty ridiculous. But it works, at least. I'm going away for a week, but will test when I get back. Thanks for the help. – djb Mar 20 '14 at 14:34
0

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.

Community
  • 1
  • 1
djb
  • 1,635
  • 3
  • 26
  • 49
  • Well if you're using Spring you probably should be using a Spring annotation to do the injection, not the JEE @EJB annotation. – Gimby Aug 13 '14 at 14:36