[英]How does dependency injection work in EJB's following polymorphism
Suppose I have an interface, let's call it A and there are two classes that implement this interface.假设我有一个接口,我们称之为 A 并且有两个类实现了这个接口。 Now, when I'll be doing dependency injection, how do I mention which specific implementation to call.
现在,当我进行依赖注入时,我如何提及要调用的特定实现。
@Local
interface SortAlgo{
void sort();
}
class bubbleSort implements SortAlgo{
void sort(){
// logic
}
}
class insertionSort implements SortAlgo{
void sort(){
// logic
}
}
Now dependency injection现在依赖注入
class SortArray{
@EJB
SortAlgo sortAlgo;
System.out.println(sortAlgo.sort());
}
At this point how I'll mention which implementation to pick.在这一点上,我将提到要选择哪个实现。
For example, if you have SMS EJB which sends SMS via Verizon or T-Mobile Gateway, then:例如,如果您有通过 Verizon 或 T-Mobile Gateway 发送 SMS 的 SMS EJB,则:
@Local
public interface SmsProvider {
public void sendSms(Sms sms);
}
T-Mobile: T移动:
@Stateless(name = "SmsProviderTMobile")
public class SmsProviderTMobile implements SmsProvider {
...
}
Verizon:威瑞森:
@Stateless(name = "SmsProviderVerizon")
public class SmsProviderVerizon implements SmsProvider {
...
}
Then you can inject specific implementation like this:然后你可以像这样注入特定的实现:
@EJB(beanName = "SmsProviderTMobile")
SmsProvider smsProviderTMobile;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.