如何将一个 EJB 3.1 注入另一个 EJB

发布于 2024-09-16 06:44:17 字数 179 浏览 2 评论 0原文

我正在开发一个简单的应用程序,其中一个 EJB 应该注入到另一个 EJB 中。我正在 IDEA Jetbrains IDE 中进行开发。但是当我在 Ejb 本地无状态类中添加 @EJB 注释后,我的 IDE 会错误地突出显示它: 未找到带有组件接口“ApplicationController”的 EJB“”。

谁能告诉为什么?

I'm developping simple app where one EJB should be injected into another. I'm developping in IDEA Jetbrains IDE. But after i make @EJB annotation in Ejb local statless class my IDE highlight it with error:
EJB '' with component interface 'ApplicationController' not found.

Can anyone tell Why?

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

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

发布评论

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

评论(2

烟酉 2024-09-23 06:44:17

可以使用@EJB 注释将一个EJB 引用注入到另一个EJB 中。以下示例取自注入其他 EJB 示例来自 OpenEJB 文档:

代码

在这个例子中我们开发了两个简单的
会话无状态 bean(DataReader
和 DataStore),并展示我们如何能够
在其中之一使用 @EJB 注释
这些豆子来获取参考
另一个会话 bean

DataStore 会话 bean

<前><代码>@无状态
公共类 DataStoreImpl 实现 DataStoreLocal、DataStoreRemote{

公共字符串 getData() {
返回“42”;
}

}

本地业务界面

<前><代码>@Local
公共接口数据存储本地{

公共字符串 getData();

}

远程业务接口

<前><代码>@Remote
公共接口数据存储远程{

公共字符串 getData();

}

DataReader 会话 bean

<前><代码>@无状态
公共类 DataReaderImpl 实现 DataReaderLocal、DataReaderRemote {

@EJB私有DataStoreRemote dataStoreRemote;
@EJB私有DataStoreLocal dataStoreLocal;

公共字符串从本地存储读取数据(){
返回“LOCAL:”+dataStoreLocal.getData();
}

公共字符串从RemoteStore读取数据(){
返回“远程:”+dataStoreRemote.getData();
}
}

注意@EJB注释的用法
在 DataStoreRemote 上和
DataStoreLocal 字段。这是
EJB 引用的最低要求
解决。如果你有两颗豆子
实施相同的业务
接口,你会想要
beanName属性如下:

@EJB(beanName = "DataStoreImpl") 
私有数据存储远程数据存储远程;

@EJB(beanName = "DataStoreImpl") 
私有数据存储本地数据存储本地;

本地业务界面

<前><代码>@Local
公共接口DataReaderLocal {

公共字符串 readDataFromLocalStore();
公共字符串 readDataFromRemoteStore();
}

(远程业务接口不
为简洁起见而显示)。

如果它没有按预期工作,也许会显示一些代码。

Injection of an EJB reference into another EJB can be done using the @EJB annotation. Here is an example taken from Injection of other EJBs Example from the OpenEJB documentation:

The Code

In this example we develop two simple
session stateless beans (DataReader
and DataStore), and show how we can
use the @EJB annotation in one of
these beans to get the reference to
the other session bean

DataStore session bean

Bean

@Stateless
public class DataStoreImpl implements DataStoreLocal, DataStoreRemote{

  public String getData() {
      return "42";
  }

}

Local business interface

@Local
public interface DataStoreLocal {

  public String getData();

}

Remote business interface

@Remote
public interface DataStoreRemote {

  public String getData();

}

DataReader session bean

Bean

@Stateless
public class DataReaderImpl implements DataReaderLocal, DataReaderRemote {

  @EJB private DataStoreRemote dataStoreRemote;
  @EJB private DataStoreLocal dataStoreLocal;

  public String readDataFromLocalStore() {
      return "LOCAL:"+dataStoreLocal.getData();
  }

  public String readDataFromRemoteStore() {
      return "REMOTE:"+dataStoreRemote.getData();
  }
}

Note the usage of the @EJB annotation
on the DataStoreRemote and
DataStoreLocal fields. This is the
minimum required for EJB ref
resolution. If you have two beans that
implement the same business
interfaces, you'll want to the
beanName attribute as follows:

@EJB(beanName = "DataStoreImpl") 
private DataStoreRemote dataStoreRemote;

@EJB(beanName = "DataStoreImpl") 
private DataStoreLocal dataStoreLocal;

Local business interface

@Local
public interface DataReaderLocal {

  public String readDataFromLocalStore();
  public String readDataFromRemoteStore();
}

(The remote business interface is not
shown for the sake of brevity).

If it doesn't work as expected, maybe show some code.

时光磨忆 2024-09-23 06:44:17

我相信这是一个 IntelliJ IDEA 错误。 此线程为我解决了问题:

添加 EJB Facet(在项目结构 > 模块中)有帮助

I believe it's an IntelliJ IDEA bug. This thread solved the problem for me:

adding a EJB Facet (in project structure > modules) helped

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