Jakarta ee 将@Inject声明为@products返回接口的具体实现
有没有办法注入Jakarta ee 将@Inject声明为@products返回接口的具体实现,jakarta-ee,interface,cdi,implementation,producer,Jakarta Ee,Interface,Cdi,Implementation,Producer,有没有办法注入@products返回接口的具体实现 SomeClassImpl implements SomeClass { public Integer iField; } 制片人级别: @Produces public SomeClass produceChild(){ SomeClassImpl impl = new SomeClassImpl(); impl.iField = 17; return impl; } 消费者类别: @Inject Some
@products
返回接口的具体实现
SomeClassImpl implements SomeClass {
public Integer iField;
}
制片人级别:
@Produces
public SomeClass produceChild(){
SomeClassImpl impl = new SomeClassImpl();
impl.iField = 17;
return impl;
}
消费者类别:
@Inject SomeClassImpl classImpl;
编辑
尝试@注入SomeClassImpl
不会强制容器使用返回超级类型SomeClass
的@products
方法
为什么可以通过
@inject
父类型(没有生产者)注入子类型,但没有变量通过@products
父类型注入子类型?您可以使用@Typed
控制bean的有效类型 如果一个interfaceSee有多个实现,请使用。关于编辑,因为@products
方法生成的是SomeClass
实例,而不是SomeClassImpl
实例,它可以返回SomeClass
的任何实现,而不仅仅是SomeClassImpl
实例。您可以通过注入SomeClass
(不是实现)和使用限定符来修复它,或者通过修改@products
方法来返回SomeClassImpl
。我得到了它,谢谢,但我需要通过products方法准确地注入SomeClassImpl类型。如果不可能-请告诉我“这不可能!”=)