Qualifiers are the mainstay of type safety and loose coupling in Contexts and Dependency Injection (CDI). Why? Without CDI, we would be injecting Java EE components in a manner similar to below
Note:This will actually not compile and is just a hypothetical code snippet
Example 1
Example 2
Whatβs wrong with the above implementations?
- Not type safe β Uses a String to specify the fully qualified name of an implementation class (see Example 1)
- Tightly couples the BasicCustomerPortal class to the BasicService class (see Example 2)
This is exactly why CDI does not do Injection this way !
Qualifiers help promote
- Loose Coupling β An explicit class is not introduced within another. Detaches implementations from each other
- Strong Typing (type safety) β No String literals to define injection properties/metadata
Qualifiers also serve as
- Binding components between beans and Decorators
- Event selectors for Observers (event consumers)
How to use Qualifiers?
CDI Qualifiers Simplified
π cdi-qualifiers-simplified
Simplified steps
- Create a Qualifier
- Apply Qualifiers to different implementation classes
- Use the Qualifiers along with @Inject to inject the instance of the appropriate implementation within a class
This was not a detailed or in-depth post about CDI Qualifiers. Itβs more of a quick reference.
More on CDI
Thanks for reading!
| Reference: | Java EE CDI Qualifiers: Quick Peek from our JCG partner Abhishek Gupta at the Object Oriented.. blog. |
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Thank you!
We will contact you soon.
Tags
CDI
π Photo of Abhishek Gupta
Abhishek GuptaApril 25th, 2014Last Updated: April 24th, 2014
Abhishek GuptaApril 25th, 2014Last Updated: April 24th, 2014
0 174 1 minute read

This site uses Akismet to reduce spam. Learn how your comment data is processed.