4

I've been struggling for this for a while now.

I'm trying to gear up for EJB 3.0. I'm using JBoss 5.1.0 GA as my application server. I started with very simple stateless session bean with local interface and a simple jsp-servlet client which invokes session bean method. All this while I've been trying to use @EJB annotation to inject session bean into the servlet class.

public class SampleServlet extends HttpServlet {

    @EJB
    private PersonLocal person;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("In servlet");       
        response.getWriter().write("Person Name : "+person.getName());
        System.out.println(person.getName());       
    }
}

I'm using JBoss 5.1.0 GA with the default configuration. (I also tried with all configuration)

However, I used to get a null reference to the session bean injection. After struggling for a day or so, I finally tried the ugly EJB 2.x JNDI lookup method instead of @EJB annotation with configuration for jndi specified in the jndi.properties file and it worked without any other changes!

Now, I tried to find out in the JBoss docs whether JBoss 5.1.0 GA does or does not support the injection with @EJB annotation, but couldn't find a concrete answer. So can someone tell me whether it does? cause I would really prefer annotation over JNDI lookup (I mean, who will not?). Am i missing something..?

Probably should have put this in the JBoss forums, but.. I'm addicted to this place ;-)

Niks
  • 4,802
  • 4
  • 36
  • 55

2 Answers2

5

This is definitely supported by JBoss 5.x since it is Java EE 5 certified.

Actually, I suggest to check the Chapter 11. Introduction to EJB injection in Servlets of the JBoss EJB3 Tutorials, they describe a detailed and step by step example.

And pay a particular attention to the following note:

For the injection to take place in a web module, your web.xml should use the 2.5 version of the web-app xsd:

<web-app version="2.5"
  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_2_5.xsd">
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 3
    I wonder how many servlet/jsp questions on SO have been solved by fixing the `web-app version`..... – skaffman Jul 21 '10 at 07:13
  • 1
    That fixed it! :) I should have read the tutorials before diving in,but anyways.. I learnt a good lesson.Thanks a lot! :) – Niks Jul 21 '10 at 07:21
  • Are there any other gotacha's like these when using normal Java clients as well? Or we can inject in Java clients as is? – Niks Jul 21 '10 at 10:58
  • 1
    DI would not work unless its an application client, and u'd need a Application Client Container: http://stackoverflow.com/questions/4168920/new-to-ejb-world-null-pointer-exception-in-ejb-client/4173972#4173972 – stratwine Apr 09 '11 at 20:17
1

I had a similiar issue using a remote EJB, but it wasn't due to the web-app version, but rather how my client was looking up the EJB.

Here's what worked for me:

//bean
@Stateless(name="xxxxService")
@Remote(xxxxRemote.class)
public class xxxxService implements xxxxRemote {

//interface - note that no annnotation is required
public interface xxxxRemote {

//in the client
  @EJB(mappedName="myJndiNameForxxxx") //for a local ejb, you could use 'beanName='
  private xxxxRemote xxxx;

For my project (built with Maven), the client is in a webapp, and the EJB is in an EJB project which is then wrapped in an EAR project. The JNDI mapping occurs in jboss.xml, which lives in the EJB project (in my project, src/main/resources/META-INF), and looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<jboss xmlns:xs="http://www.jboss.org/j2ee/schema"
       xs:schemaLocation="http://www.jboss.org/j2ee/schema/jboss_5_0.xsd"
       version="5.0">
  <enterprise-beans>
  <session>
      <ejb-name>xxxxService</ejb-name>
      <jndi-name>myJndiNameForxxxx</jndi-name>
  </session>
  </enterprise-beans>
</jboss>

That's all it took, and now I can access the EJB from the client. Hope this helps someone.

If you are still having issues you can look in the JMX console to make sure the JNDI entry shows up. Look under 'jboss'>'service=JNDIview'>'list' and click the 'invoke' button. If your service is deployed correctly, you should see the JNDI name (in my example, myJndiNameForxxxx) under 'ProxyFactory'.

MattC
  • 5,874
  • 1
  • 47
  • 40
  • Your answer is not very relevant to the question asked, but still as you said, might be helpful to someone! – Niks Apr 15 '13 at 08:20