有没有办法注入@Produces
返回接口的具体实现?
SomeClassImpl implements SomeClass {
public Integer iField;
}
制作人类:
@Produces
public SomeClass produceChild(){
SomeClassImpl impl = new SomeClassImpl();
impl.iField = 17;
return impl;
}
消费者类:
@Inject SomeClassImpl classImpl;
修改
@Inject SomeClassImpl
的尝试不会强制容器使用返回超类型@Produces
的{{1}}方法。
为什么可以通过SomeClass
父类型(没有生产者)注入子类型,但没有变体通过@Inject
父类型注入子类型?
答案 0 :(得分:1)
您可以使用@Typed
来控制bean的有效类型。 https://docs.jboss.org/cdi/api/1.0/javax/enterprise/inject/Typed.html