Glassfish Web 应用程序中的 EJB 注入

发布于 2024-09-01 16:06:47 字数 303 浏览 5 评论 0原文

我有一个应用程序尝试使用 @EJB 注释将远程引用注入到我的 ejb.jar 文件中的 EJB。我得到的结果不一致。在一种情况下,我在 web.xml 中有一个监听器被调用,并且显然已正确注入 EJB,因为我可以看到它连接到 EJB 并调用其上的方法。在另一个类(struts2 操作)中,当它尝试访问 EJB 引用时,我得到 NPE。据我所知,它们是相同的调用,位于同一个 .war 文件中的 Java 类中。

作为解决方法,我在构造函数中添加了代码以通过全局 JNDI 名称查找 EJB,并且效果很好。我只是不明白为什么一个 @EJB 有效,而另一个 @EJB 不起作用。

I've got an app that is trying to use @EJB annotation to inject remote references to EJBs in my ejb.jar file. I'm getting inconsistent results. In one case, I have a listener in web.xml that gets called and apparently has the EJB injected correctly, since I can see it connecting to the EJB and calling methods on it. In another class (a struts2 action) I get NPE when it tries to access the EJB reference. As far as I can tell, they're identical calls, in Java classes that live in the same .war file.

As a work around, I added code in the constructor to lookup the EJBs by their global JNDI names, and it works fine. I just can't figure out why one @EJB works and not another.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-09-08 16:06:47

如何在 Struts 2 操作中注入 EJB?您使用 CDI 吗?您是否使用 Struts2 CDI 插件

更新:问题在于容器没有创建 Struts 对象,而 Struts 则创建了对象,因此容器没有机会注入任何内容。您必须使用提到的 插件CDI 在您的操作中启用注入。

如果您想尝试一下,请获取 Struts 2 源代码:

svn co http://svn.apache.org/repos/asf/struts/struts2/trunk/ struts2

然后 cd 进入 struts2 目录并运行以下命令(这将编译 >struts-cdi-plugin)

mvn install -pl plugins -am

然后获取 cdi-plugin 的源代码:

svn co https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-cdi-plugin/

并编译它:

mvn install

现在,通过我的 pom.xml 中的以下依赖项:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.2.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-cdi-plugin</artifactId>
    <version>2.2.0-SNAPSHOT</version>
</dependency>
<dependency>
  <groupId>javassist</groupId>
  <artifactId>javassist</artifactId>
  <version>3.8.0.GA</version>
</dependency>

我能够在 Action 中注入 EJB:

public class HelloWorld extends ActionSupport {

    @Inject
    HelloEJB helloEjb;

    @Override
    public String execute() throws Exception {
        setMessage(helloEjb.getMessage());
        return SUCCESS;
    }

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}

请参阅https://svn.apache.org/repos /asf/struts/sandbox/trunk/struts2-cdi-example/ 为例。

How do you inject EJBs in Struts 2 actions? Are you using CDI? Are you using the Struts2 CDI plugin?

Update: The problem is that the container is not creating the Struts objects, Struts is, so the container doesn't get the opportunity to inject anything. You'll have to use the mentioned plugin for CDI to enable injection in your actions.

If you want to give it a try, get Struts 2 sources:

svn co http://svn.apache.org/repos/asf/struts/struts2/trunk/ struts2

Then cd into the struts2 directory and run the following command (this will compile the required modules for the struts-cdi-plugin)

mvn install -pl plugins -am

Then get the sources of the cdi-plugin:

svn co https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-cdi-plugin/

And compile it:

mvn install

Now, with the following dependencies in my pom.xml:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.2.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-cdi-plugin</artifactId>
    <version>2.2.0-SNAPSHOT</version>
</dependency>
<dependency>
  <groupId>javassist</groupId>
  <artifactId>javassist</artifactId>
  <version>3.8.0.GA</version>
</dependency>

I was able to get an EJB injected in an Action:

public class HelloWorld extends ActionSupport {

    @Inject
    HelloEJB helloEjb;

    @Override
    public String execute() throws Exception {
        setMessage(helloEjb.getMessage());
        return SUCCESS;
    }

    private String message;

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}

See https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2-cdi-example/ for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文