选择EJB进行注入,无需重新编译 [英] Choose EJB to be injected without recompiling

查看:111
本文介绍了选择EJB进行注入,无需重新编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有两个实现的 @Local 界面

Imagine you have two implementations of a @Local interface

@Local
public interface LocalInterface {
}

@Stateless
public class MyFirstImplementation implements LocalInterface {
}

@Stateless
public class MySecondImplementation implements LocalInterface {
}

我想选择, 不重新编译项目(即在运行时或使用外部配置属性)我想使用哪一个(MyFirstImplementation或MySecondImplementation)。

And I want to choose, without recompiling the project (that is, at runtime or using an external configuration property) which one (MyFirstImplementation or MySecondImplementation) I want to use.

public class MyClass {
   @EJB
   LocalInterface local;
}

选择一个实现后,不必更改。我正在使用JBoss 5.1,如果这有帮助。

Once one implementation is chosen, it does not have to change. I am using JBoss 5.1 if that helps.

推荐答案

您可以使用部署描述符来实现 - ejb-jar.xml 。这样的(可能不是100%准确,但我想你已经得到了点):

You can achieve it using the deployment descriptor - ejb-jar.xml. Something like this (might not be 100% accurate, but I think you've got the point):

<ejb-jar> 
   <enterprise-beans>
      <session>
         <ejb-name>MyClass</ejb-name>
         <ejb-ref>
            <ejb-ref-name>ejb/myLocalReferencedBean</ejb-ref-name>
            <ejb-ref-type>Session</ejb-ref-type>
            <local>com.yourpackage.LocalInterface</local>
            <ejb-link>MyFirstImplementation</ejb-link>
            <injection-target>local</injection-target>
         </ejb-ref>
      </session>

      <session>
         <ejb-name>MyFirstImplementation</ejb-name>
         <!-- ... -->
      </session>
      <session>
         <ejb-name>MySecondImplementation</ejb-name>
         <!-- ... -->
      </session>
   </enterprise-beans>
</ejb-jar>

另一种方法是使用如下所述的CDI:根据条件注入@EJB bean

Another way is to use the CDI as described here: Inject @EJB bean based on conditions

这篇关于选择EJB进行注入,无需重新编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆