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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3576327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:29:18  来源:igfitidea点击:

How to inject one EJB 3.1 into another EJB

javajbossintellij-ideajava-ee-6ejb-3.1

提问by breedish

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.

我正在开发一个简单的应用程序,其中一个 EJB 应该注入另一个。我正在 IDEA Jetbrains IDE 中进行开发。但是在我在 Ejb 本地 statless 类中制作 @EJB 注释后,我的 IDE 突出显示它并显示错误:EJB '' with component interface 'ApplicationController' not found。

Can anyone tell Why?

谁能告诉为什么?

回答by Pascal Thivent

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

可以使用@EJB注释将 EJB 引用注入另一个 EJB 。下面是从 OpenEJB 文档中的注入其他 EJB 示例中获取的示例

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 @EJBannotation 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).

代码

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

数据存储会话 bean

豆角,扁豆

@Stateless
public class DataStoreImpl implements DataStoreLocal, DataStoreRemote{

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

}

本地业务接口

@Local
public interface DataStoreLocal {

  public String getData();

}

远程业务接口

@Remote
public interface DataStoreRemote {

  public String getData();

}

DataReader 会话 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();
  }
}

请注意@EJBDataStoreRemote 和 DataStoreLocal 字段上注释的用法。这是 EJB 引用解析所需的最低要求。如果您有两个实现相同业务接口的 bean,您将需要 beanName 属性,如下所示:

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

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

本地业务接口

@Local
public interface DataReaderLocal {

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

(为简洁起见,未显示远程业务界面)。

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

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

回答by Matthew Cornell

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

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

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

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