Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jakarta ee TomEE如何实现依赖注入?_Jakarta Ee_Tomcat_Interface_Dependency Injection_Apache Tomee - Fatal编程技术网

Jakarta ee TomEE如何实现依赖注入?

Jakarta ee TomEE如何实现依赖注入?,jakarta-ee,tomcat,interface,dependency-injection,apache-tomee,Jakarta Ee,Tomcat,Interface,Dependency Injection,Apache Tomee,我找到了这个例子 它声称是依赖注入的一个例子。我看到了多个接口,在这些接口实现之后,我看不到@EJB实现是如何使用DI的。它看起来就像三种不同的类型使用不同的接口 我希望看到一个接口和许多实现它的不同类,并通过构造或setter传递/注入到示例中的DataReader类 这个示例如何显示依赖项注入 (我应该从另一个网站发布代码吗?) 这是DataReader类: import javax.ejb.EJB; import javax.ejb.Stateless; /** * This is a

我找到了这个例子

它声称是依赖注入的一个例子。我看到了多个接口,在这些接口实现之后,我看不到@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();
    }
}
导入javax.ejb.ejb;
导入javax.ejb.Stateless;
/**
*这是一个EJB3.1风格的pojo无状态会话bean
*必须对每个无状态会话bean实现进行注释
*使用注释@Stateless
*此EJB有2个业务接口:DataReaderRemote,一个远程业务接口
*接口,以及本地业务接口DataReaderLocal
*

*实例变量“dataStoreRemote”使用@EJB注释进行注释: *这意味着应用服务器在运行时将注入此实例 *变量是对EJB数据存储远程的引用 *

*实例变量“dataStoreLocal”使用@EJB注释进行注释: *这意味着应用服务器在运行时将注入此实例 *变量:对EJB数据存储本地的引用 */ //开始代码段:代码 @无国籍 公共类数据读取器{ @EJB 私有数据存储远程数据存储远程; @EJB 私有数据存储本地数据存储本地; @EJB 私有数据存储; 公共字符串readDataFromLocalStore(){ 返回“LOCAL:+dataStoreLocal.getData(); } 公共字符串readDataFromLocalBeanStore(){ 返回“LOCALBEAN:+dataStore.getData(); } 公共字符串readDataFromRemoteStore(){ 返回“REMOTE:+dataStoreRemote.getData(); } }


之所以称之为DI,是因为您在不知道是哪个实现的情况下被注入了一个实现。没错,该示例没有提供相同接口的N个实现,以避免使其过于复杂,目的只是展示注入是如何工作的