Java EE CDI Qualifiers: Quick Peek

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

cdi1

 

Example 2

cdi2

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 two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 5



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close