【问题标题】:How does TomEE implement Dependency Injection?TomEE 如何实现依赖注入?
【发布时间】: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


    【解决方案1】:

    这是 DI 只是因为您在不知道它是哪一个的情况下注入了一个实现。没错,这个例子并没有提供同一个接口的N个实现,以免太复杂,目的只是为了展示注入是如何工作的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2014-01-08
      相关资源
      最近更新 更多