Java ee接口条件注入 [英] Java ee interface conditional inject
问题描述
我有以下界面:
public interface ResultEvaluationInterface {
public void evaluateResults(Event e);
}
我希望根据我的 Event.type
具有相同实现的不同类。类似的东西:
and I want to inject in my class depending on my Event.type
different classes with the same implementation. Something like that:
@Stateless
@LocalBean
public class ResultEvaluation implements ResultEvaluationInterface {
@Override
public void evaluateResults(Event e) {
switch (e.getType()) {
case Type.Running:
// inject and call ResultEvaluationRunningEJB.evaluateResults(e)
case Type.Swimming:
// inject and call ResultEvaluationSwimmingEJB.evaluateResults(e)
default:
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
ResultEvaluationRunningEJB
和 ResultEvaluationSwimmingEJB
都实现了该接口。任何人都知道如何以一种好的方式做到这一点?
ResultEvaluationRunningEJB
and ResultEvaluationSwimmingEJB
both implement the interface. Anybody has got a good idea how to do that in a good way?
推荐答案
如果你真的想使用硬编码的话在prod和dev事件之间切换的声明你可以使用CDI限定符只是将两个实现注入Facade:
If you really want to use a hard coded if statement to switch between prod and dev events you could use CDI Qualifiers simply inject the two implementations into a Facade:
@Stateless
@LocalBean
public class ResultEvaluationFacade {
@Inject
@Development
private ResultEvalutationInterface dev;
@Inject
@Production
private ResultEvalutionInterface prod;
@Override
public void evaluateResults(Event e) {
switch (e.getType()) {
case Type.Production:
prod.evaluteResult(e);
break;
case Type.Development:
dev.evaluteResult(e);
break;
default:
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
并定义你的两个实现:
@Development
public class ResultEvaluationDevelopment implements ResultEvaluationInterface {
...
}
@Production
public class ResultEvaluationDevelopment implements ResultEvaluationInterface {
...
}
但是我会考虑使用模拟maven项目代替两个单独的实现。
However I would consider using a mock maven project to house the two separate implementations instead.
或者你可以使用不同的CDI事件类型,类似这样。
Alternatively you could use different CDI Event types, something like this.
public void observeDevEvent(@Observe DevEvent event) {
//do stuff.
}
public void observeProdEvent(@Observe ProdEvent event) {
//do stuff
}
点燃事件将如下所示:
@Inject
private Event<ProdEvent> prodEvent;
public void someMethod() {
ProdEvent pe = new ProdEvent()
// set some data on ProdEvent
prodEvent.fire(pe);
}
注意事件也适用于限定符,因此您还可以添加限定符注释到事件而不是实现两种不同类型的事件。
Note events can also work with Qualifiers, so you could also add a Qualifier annotation to the Event instead of implementing two different types of event.
@Inject
@Production
private Event<MyEvent> event;
并收听@Prodcution事件;
And listen for @Prodcution events;
public void handleProdEvent(@Observer @Production MyEvent myEvent) {
// do Stuff.
}
对于bean的懒惰实例化,您可以使用CDI实例注入。
For lazy instantiation of beans you can use CDI Instance injection.
@Inject
private Instance<BeanA> beanA;
....
public void doStuff(Event e) {
...
case Type.Production:
//lazily evaluates and instantiatiates bean.
beanA.get().evaluateResult(e);
}
这篇关于Java ee接口条件注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!