gpt4 book ai didi

java - 如何将一个 EJB 3.1 注入(inject)另一个 EJB

转载 作者:搜寻专家 更新时间:2023-11-01 04:01:36 25 4
gpt4 key购买 nike

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

谁能说出为什么?

最佳答案

可以使用 @EJB 注释将 EJB 引用注入(inject)到另一个 EJB 中。这是取自 Injection of other EJBs Example 的示例来自 OpenEJB 文档:

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

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

关于java - 如何将一个 EJB 3.1 注入(inject)另一个 EJB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3576327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com