@EJB in JBoss 5.1.0 GA?
I've been struggling fo开发者_如何学Gor 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 ;-)
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">
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'.
精彩评论