All CDI documentation makes it clear that CDI does impose dependency injection - and thatβs a high CDI value. IMHO, what you are trying to do is exactly what CDI is trying to avoid. You want the container to use Object for each type, and CDI does not work that way.
The injection points stringValue and integerValue can only accept a bean that has java.lang.String and java.lang.Integer in its list of bean types, respectively. java.lang.Object does not meet this criterion.
I have two suggestions. First, since you have two or more injection points of different types, create two or more creation methods for these types:
public class TestProducer { @Produces @TestQualifier public String createString(InjectionPoint ip) { if(something) { return "a String"; } else {
It works if the condition of something is to simply check the type of injection point (what I'm betting on).
However, if the something condition determines the type using different criteria than the type of the injection point, I suggest that you do the dirty work yourself: enter the return value in the Object -typed injection and make a manual throw:
@Named("test") public class TestComponent { ... @Inject public void setA(@TestQualifier Object value) { String stringValue = (String) value; ... @Inject public void setB(@TestQualifier Object value) { int intValue = (Integer) value;
The main thing is that, unlike some other DI structures, CDI does not work against a system like Java - on the contrary, it uses it heavily. Do not try to deal with this, but use this aspect of CDI to your advantage :)
source share