我遇到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()
的{{1}},我RoleCondition
需要@MyOwnQualifier
。
但此时有些人还没有被CDI注射过。
如何让这条线路起作用?
@Inject
@MyOwnQualifier(condition = someBean.getSomeCondition()) // not work because some is not already injected at this point
private BeanInterface myEJB;
如何使用基于另一个注入属性值的限定符来动态注入bean?
答案 0 :(得分:4)
试试这个......
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");
}