4

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?

4

2 回答 2

14

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

编码

在此示例中,我们开发了两个简单的会话无状态 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();
  }
}

@EJB请注意DataStoreRemote 和 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();
}

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

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

于 2010-08-26T18:22:53.157 回答
5

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

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

于 2012-08-20T17:54:21.660 回答