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.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


1 × eight =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books