将@Inject 声明为@Produces 返回接口的具体实现

Declare @Inject as concrete implementation of @Produces returned interface

有什么方法可以注入 @Produces 返回接口的具体实现吗?

SomeClassImpl implements SomeClass {
    public Integer iField;
}

制作人class:

@Produces
public SomeClass produceChild(){
    SomeClassImpl impl = new SomeClassImpl();
    impl.iField = 17;
    return impl;
}

消费者class:

@Inject SomeClassImpl classImpl;

编辑

尝试 @Inject SomeClassImpl 不会强制容器使用 returns 超类型 SomeClass.

@Produces 方法

为什么可以通过 @Inject 父类型(没有 Producer)注入子类型,但没有变体通过 @Produces 父类型注入子类型?

您可以使用 @Typed 来控制您的 bean 的有效类型。 https://docs.jboss.org/cdi/api/1.0/javax/enterprise/inject/Typed.html