jakarta-ee - TomEE 是如何实现依赖注入(inject)的?

标签 jakarta-ee tomcat interface dependency-injection apache-tomee

我找到了这个例子,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/

相关文章:

tomcat - JasperReports servlet 依赖项

interface - Jitterbit 与 BizTalk

java - java抽象接口(interface)中的用户对象

java - Neo4j跨类获取多个数据库实例

java - JDBC 领域身份验证错误 Glassfish 4

java - 如何配置eclipse支持java?

TOMCAT 6 SSL 错误 : Alias name does not identify a key entry

jakarta-ee - 您可以在 Web 应用程序的 Context.xml 文件中声明非原始对象吗?

Spring 启动 : How to add another WAR files to the embedded tomcat?

java - 谁能解释一下这段代码中 javax.swing.SwingUtilities.invokeLater 的用法