First I should clarify that this post is not intended to criticize CDI, but to discover the thinking and assumptions behind the design of CDI and that will have obvious influence on designing any web app, which uses CDI.
One of the most distinguished feature of CDI (of Java EE 6) is type safety. Jboss Seam was not safe in type. It uses name to qualify any instance to inject. Like bellow:
@Name("myBean")
public class MyBean implements Bean {
...
}
@Name("yourBean")
public class YourBean implements Bean {
...
}
While injecting MyBean one can do this:
@In
private Bean myBean; //myBean is injected
@In
private Bean yourBean; //yourBean is injected
And earlier versions of Spring (before 3.0), this type of injection happened like bellow:
Just define the beans in bean configuration file:
<bean id="myBean" class="com.example.common.MyBean">
...
</bean>
<bean id="yourBean" class="com.example.common.YourBean">
...
</bean>
And use named qualifier, deciding which one to use:
@Autowired
@Qualifier("myBean")
private Bean bean;
@Autowired
@Qualifier("yourBean")
private Bean bean;
But now in CDI, First you need to define a custom Qualifier
annotation for any specific type of object. Then use that annotation for qualifying that object. At the end of the day, when you look at your source code, you see that, you wasted considerable amount of time to write lots of custom annotations for dependency injection. Java community is moving towards annotations, leaving XML based configurations (verbose XML) behind. Is there anything that would convince anyone to think this (type safety with custom annotations) not as verbose annotations, but as an excellent and distinguished feature of CDI?
Edit:
Points, pushed to be highlighted
- If I use custom qualifier for type safety per service or dao (per interface), then for a large sized application like having 1000 or more service or dao classes with multiple implementations, it will be messy. Then for large sized applications, Is that feasible to use type safe injection?
- If the answer of the above question is "No" then, what is the point to use type safety?
- Even if it is feasible to write annotations for type safety, in large applications, is it really worth the effort for just avoiding verbose xml configuration?
- When actually I need type safety instead of bean name qualifier?
Short discussion on the above points
- There are not too many cases when you actually need type safe injection, specially when you have one implementation of an interface, you should use
@Name
as the qualifier. So yes, in a large sized application it is feasible to use type safety when it is actually needed.
- Ofcourse type safety is one of the distinguished feature of CDI and in the accepted answer there is a non-exhaustive list of reasons why you may chose to use type safety.
- As you are an intelligent programmer and you know precisely when to use type safety, so definitely it worth the effort, when really needed.
- Most of the parts of the accepted answer really talks, when do we need type safety and this article is also very helpful to understand.
Thanks and happy coding!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…