Jakarta ee 基于@Inject字段CDI使用@Qualifier

Jakarta ee 基于@Inject字段CDI使用@Qualifier,jakarta-ee,ejb,cdi,qualifiers,Jakarta Ee,Ejb,Cdi,Qualifiers,我的CDI有问题 注入在EJB的注入中使用一种策略 我的实际情况是: public class someManagedBean { @Inject @MyOwnQualifier(condition = someBean.getSomeCondition()) // not work because someBean is not already injected at this point private BeanInterface myEJB; @Inj

我的CDI有问题 注入在EJB的注入中使用一种策略

我的实际情况是:

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()
返回我的
@MyOwnQualifier
所需的
rolecodition
。 但此时CDI尚未注入someBean

我怎样才能使这条线起作用

@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");
}
试试这个

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");
}

我真的不明白这是怎么回事,因为注释是编译时的东西,而someBean.getSomeCondition()在运行时求值。我认为您更希望创建一个生产者,它可以有条件地提供一个实例。在尝试注入之前,没有像
beforeConstruct
这样的东西来计算某些值吗?我真的不知道这是如何工作的,因为注释是编译时的东西,而someBean.getSomeCondition()在运行时计算。我认为您更希望创建一个生产者,它可以有条件地提供一个实例。在尝试注入之前,没有类似于在构建之前评估某些值的
这样的事情吗?首先,感谢您的帮助。此时,由于某种原因,工厂上的someBean为null
getBeanInterfaceEJBImpl
如果为null意味着BeanInterfaceFactory不是CDIBean,请尝试添加@Named。你在beans.xml中使用的是哪种发现模式?是的,很抱歉我的无知哈哈,午饭后我会检查一切是否正常,显然它会工作。非常感谢你,你是最棒的:)我很高兴能帮助你!;-)谢谢你,伙计,它真的很有魅力。。我只需要将
新BeanInterfaceEJBImpl()
替换为
CDI.current().select(BeanInterfaceEJBImpl.class).get()首先,感谢您的帮助。此时,由于某种原因,工厂上的someBean为null
getBeanInterfaceEJBImpl
如果为null意味着BeanInterfaceFactory不是CDIBean,请尝试添加@Named。你在beans.xml中使用的是哪种发现模式?是的,很抱歉我的无知哈哈,午饭后我会检查一切是否正常,显然它会工作。非常感谢你,你是最棒的:)我很高兴能帮助你!;-)谢谢你,伙计,它真的很有魅力。。我只需要将
新BeanInterfaceEJBImpl()
替换为
CDI.current().select(BeanInterfaceEJBImpl.class).get()