【发布时间】:2014-06-03 20:10:51
【问题描述】:
我找到了这个例子,http://tomee.apache.org/examples-trunk/injection-of-ejbs/README.html
它声称是依赖注入的一个例子。在实现这些接口之后,我看到了多个接口,我看不到 @EJB 实现如何使用任何 DI。它看起来就像使用不同接口的三种不同类型。
我希望看到一个接口和许多实现它的不同类和在示例中通过构造或设置器传递/注入到 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();
}
}
【问题讨论】:
标签: jakarta-ee tomcat interface dependency-injection apache-tomee