简体   繁体   English

JBoss 5.1.0 GA中的@EJB?

[英]@EJB in JBoss 5.1.0 GA?

I've been struggling for this for a while now. 我一直在为此奋斗一段时间。

I'm trying to gear up for EJB 3.0. 我正在尝试为EJB 3.0做好准备。 I'm using JBoss 5.1.0 GA as my application server. 我正在使用JBoss 5.1.0 GA作为我的应用程序服务器。 I started with very simple stateless session bean with local interface and a simple jsp-servlet client which invokes session bean method. 我从非常简单的无状态会话bean开始,使用本地接口和一个简单的jsp-servlet客户端调用会话bean方法。 All this while I've been trying to use @EJB annotation to inject session bean into the servlet class. 所有这一切,我一直在尝试使用@EJB注释将会话bean注入servlet类。

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. 我正在使用JBoss 5.1.0 GA和默认配置。 (I also tried with all configuration) (我也试过所有配置)

However, I used to get a null reference to the session bean injection. 但是,我曾经得到会话bean注入的空引用。 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! 经过一天左右的努力,我终于尝试了丑陋的EJB 2.x JNDI查找方法而不是@EJB注释,并在jndi.properties文件中指定了jndi的配置,它没有任何其他更改!

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. 现在,我试图在JBoss文档中找出JBoss 5.1.0 GA是否支持@EJB注释的注入,但是找不到具体的答案。 So can someone tell me whether it does? 那么有人可以告诉我它是否呢? cause I would really prefer annotation over JNDI lookup (I mean, who will not?). 因为我真的更喜欢注释而不是JNDI查找(我的意思是,谁不会?)。 Am i missing something..? 我错过了什么......?

Probably should have put this in the JBoss forums, but.. I'm addicted to this place ;-) 可能应该把它放在JBoss论坛中,但是......我沉迷于这个地方;-)

This is definitely supported by JBoss 5.x since it is Java EE 5 certified. 这是JBoss 5.x肯定支持的,因为它已通过Java EE 5认证。

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. 实际上,我建议查看第11章 JBoss EJB3教程的 Servlet中的EJB注入简介 ,它们描述了一个详细的分步示例。

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模块中进行注入,您的web.xml应使用2.5版本的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. 我有一个使用远程EJB的类似问题,但它不是由于Web应用程序版本,而是我的客户端如何查找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. 对于我的项目(使用Maven构建),客户端位于webapp中,EJB位于EJB项目中,然后将其包装在EAR项目中。 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: JNDI映射发生在jboss.xml中,它存在于EJB项目中(在我的项目中,src / main / resources / META-INF),如下所示:

<?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. 这就是所有,现在我可以从客户端访问EJB。 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. 如果您仍然遇到问题,可以查看JMX控制台以确保JNDI条目显示。 Look under 'jboss'>'service=JNDIview'>'list' and click the 'invoke' button. 在“jboss”>“service = JNDIview”>“列表”下查看,然后单击“调用”按钮。 If your service is deployed correctly, you should see the JNDI name (in my example, myJndiNameForxxxx) under 'ProxyFactory'. 如果您的服务部署正确,您应该在'ProxyFactory'下看到JNDI名称(在我的示例中,myJndiNameForxxxx)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM