我按照此处的示例动态选择在运行时注入的实现。然后我尝试根据我的理解来实现它,但我的代码总是返回默认实现;
这是我的代码
@Stateless
public class MemberRegistration {
@Inject
private Logger log;
@Inject
private EntityManager em;
@Inject
private Event<Member> memberEventSrc;
@Inject
@Switch
IHandler handler;
private int handlerValue;
public String testCDI(int value) {
handlerValue = value;
log.info("handling " + value);
log.info("handling " + handlerValue);
return handler.handle();
}
@Produces
@RequestScoped
@Switch
public IHandler produceHandler(@New Handler0 handler0,
@New Handler1 handler1) {
log.info("Calling producer method with handler: "+handlerValue);
switch (handlerValue) {
case 1:
log.info("returning one");
return handler1;
case 0:
log.info("returning 0");
return handler0;
default:
log.info("returning default");
return handler1;
}
}
}
当我调用该方法时,testCDI
我会更新该方法,handlerValue
以便我的生产者方法可以使用该值。我在这里缺少什么来确保在正确的值可用时调用生产者方法?
代码在 Wildfly 8.2.0 上运行