@EJB in JBoss 5.1.0 GA?

I fought for this for a while.

I am trying to configure EJB 3.0. I am using JBoss 5.1.0 GA as my application server. I started with a very simple stateless bean session with a local interface and a simple jsp-servlet client that calls the bean session method. All this while I tried to use the @EJB annotation to inject a bean session 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 am using JBoss 5.1.0 GA with the default setting. (I also tried with the whole configuration)

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

Now I tried to find out in the JBoss docs whether JBoss 5.1.0 supports GA or does not support injection with @EJB annotation, but cannot find a specific answer. So can someone tell me right? because I would prefer annotation to JNDI search (I mean, who won't?). Am I missing something ..?

This should probably have been a nuisance on the JBoss forums, but .. I was addicted to this place; -)

+4
source share
2 answers

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

In fact, I suggest checking out Chapter 11. Introduction to EJB Injection in Servlets JBoss EJB3 Tutorials , they describe a detailed and step-by-step example.

Pay particular attention to the following note:

For injection into the web module, your web.xml should use the 2.5 version of the xsd web application:

 <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"> 
+5
source

I had a similar problem using remote EJB, but this was not due to the version of the web application, but from the way my client was browsing the EJB.

Here is 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 using Maven), the client is in the webapp, and the EJB is in the EJB project, which is then wrapped in an EAR project. JNDI mapping happens in jboss.xml, which lives in an 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 takes, and now I can access the EJB from the client. Hope this helps someone.

If you still have problems, you can look in the JMX console to make sure that the JNDI entry is displayed. Look under "jboss"> "service = JNDIview"> "list" and click on the "invoke" button. If your service is deployed correctly, you should see the JNDI name (in my example, myJndiNameForxxxx) in the "ProxyFactory" section.

+1
source

Source: https://habr.com/ru/post/1316276/


All Articles