【问题标题】:Inject EJBs to a List by common interface通过通用接口将 EJB 注入列表
【发布时间】:2014-02-05 11:01:42
【问题描述】:

我想自动从 EJB 容器中查找 EJB 实例并将其注入到列表中。示例:

@EJBs
List<MyCommonInterface> beans;

或

List<MyCommonInterface> beans;

public MyClass() {
    beans = (List<MyCommonInterface>) context.findBeansByInterface(MyCommonInterface.class);
}

这可能吗?

【问题讨论】:

    标签: java jakarta-ee dependency-injection ejb jndi


    【解决方案1】:

    使用 JEE6,您可以使用实例 http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/Instance.html

    这样您就可以使用注入的上下文,例如

    @Inject
    private Instance<ICommon> ejbs;
    

    您可以对其进行迭代:

        for(ICommon c : ejbs)
            c.doSomeThing();
    

    这里有一个这样的例子

      public interface ICommon {
          void doSomeThing();   
      }
    

    我在@Singleton 中使用 SLSB 和 SFSB 对其进行了测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-21
      • 2012-05-11
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多