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

时间:2016-05-15 12:46:23

标签: java-ee interface cdi implementation producer

有没有办法注入@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父类型注入子类型?

1 个答案:

答案 0 :(得分:1)

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

相关问题