我找到了这个例子,http://tomee.apache.org/examples-trunk/injection-of-ejbs/README.html
它声称是依赖注入(inject)的一个例子。我看到多个接口(interface),在实现这些接口(interface)之后,我看不到 @EJB 实现如何使用任何 DI。它看起来就像使用不同接口(interface)的三种不同类型。
我希望看到一个接口(interface)和许多实现它的不同类和被传递/注入(inject)示例中的 DataReader 类,通过构造或 setter。
此示例如何显示依赖注入(inject)?
(我应该从另一个网站发布代码吗?)
这是 DataReader 类:
import javax.ejb.EJB;
import javax.ejb.Stateless;
/**
* This is an EJB 3.1 style pojo stateless session bean
* Every stateless session bean implementation must be annotated
* using the annotation @Stateless
* This EJB has 2 business interfaces: DataReaderRemote, a remote business
* interface, and DataReaderLocal, a local business interface
* <p/>
* The instance variables 'dataStoreRemote' is annotated with the @EJB annotation:
* this means that the application server, at runtime, will inject in this instance
* variable a reference to the EJB DataStoreRemote
* <p/>
* The instance variables 'dataStoreLocal' is annotated with the @EJB annotation:
* this means that the application server, at runtime, will inject in this instance
* variable a reference to the EJB DataStoreLocal
*/
//START SNIPPET: code
@Stateless
public class DataReader {
@EJB
private DataStoreRemote dataStoreRemote;
@EJB
private DataStoreLocal dataStoreLocal;
@EJB
private DataStore dataStore;
public String readDataFromLocalStore() {
return "LOCAL:" + dataStoreLocal.getData();
}
public String readDataFromLocalBeanStore() {
return "LOCALBEAN:" + dataStore.getData();
}
public String readDataFromRemoteStore() {
return "REMOTE:" + dataStoreRemote.getData();
}
}
最佳答案
它是 DI 只是因为您在不知道它是哪一个的情况下被注入(inject)了一个实现。没错,这个例子没有提供同一个接口(interface)的 N 个实现,以免让它变得太复杂,目的只是为了展示注入(inject)是如何工作的。
关于jakarta-ee - TomEE 是如何实现依赖注入(inject)的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024077/