I'm having trouble with CDI conditional injection to use a kind of Strategy in EJB's injections.
My actual scenario is that:
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");
}
Ok, scenario explained. Now the problem is that I need to get the value
from someBean.getSomeCondition()
that returns a RoleCondition
, necessary for my @MyOwnQualifier
.
But at this time someBean is not already injected by CDI.
How I can make this line to work?
@Inject
@MyOwnQualifier(condition = someBean.getSomeCondition()) // not work because some is not already injected at this point
private BeanInterface myEJB;
How is the right way to dynamically inject beans using qualifiers based on another injection's property value?
Try this...
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");
}