使用基于@Inject 字段CDI 的@Qualifier

Using @Qualifier based on @Inject field CDI

我在使用 CDI 条件时遇到问题 injection 在EJB的注入中使用一种Strategy。

我的实际情况是:

public class someManagedBean {

    @Inject 
    @MyOwnQualifier(condition = someBean.getSomeCondition()) // not work because someBean is not already injected at this point
    private BeanInterface myEJB;

    @Inject
    SomeBean someBean;
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyOwnQualifier {
    SomeCondition condition();
}

public class BeanInterfaceFactory {
    @Produces
    @MyOwnQualifier(condition = RoleCondition.ADMIN) 
    public BeanInterface getBeanInterfaceEJBImpl() {
        return new BeanInterfaceEJBImpl();
    }
}

public enum RoleCondition {
    ADMIN("ADMIN User");
}

好的,场景说明。现在的问题是我需要获得价值 来自 someBean.getSomeCondition() 那 returns 一个 RoleCondition,对我的 @MyOwnQualifier 来说是必要的。 但是此时someBean还没有被CDI注入。

我怎样才能使这条线工作?

@Inject 
@MyOwnQualifier(condition = someBean.getSomeCondition()) // not work because some is not already injected at this point
private BeanInterface myEJB;

根据另一个注入的 属性 值使用限定符动态注入 bean 的正确方法是什么?

试试这个...

public class someManagedBean {

    @Inject 
    @MyOwnQualifier
    private BeanInterface myEJB;
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface MyOwnQualifier {
    SomeCondition condition();
}

public class BeanInterfaceFactory {

    @Inject
    SomeBean someBean

    @Produces
    @MyOwnQualifier
    public BeanInterface getBeanInterfaceEJBImpl() {
        if(someBean.getCondition()) {         
            return new BeanInterfaceEJBImpl();
        } else {
           ....
        }
    }
}

public enum RoleCondition {
    ADMIN("ADMIN User");
}