我找到了这个例子,http://tomee.apache.org/examples-trunk/injection-of-ejbs/README.html
它声称是依赖注入的一个例子。在实现这些接口后,我看到多个接口,我看不出@EJB实现如何使用任何DI。它看起来像使用不同接口的三种不同类型。
我希望看到一个接口和许多实现它的不同类和通过构造或setter传递/注入到示例中的DataReader类。
此示例如何显示依赖注入?
(我应该从其他网站发布代码吗?)
这是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();
}
}
答案 0 :(得分:1)
这是DI,因为你注入了一个实现而不知道它是哪一个。这是正确的,该示例不提供相同界面的N个实现,以免使其过于复杂,目的只是展示注射的工作原理。