Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java EE CDI Qualifiers: Quick Peek

DZone's Guide to

Java EE CDI Qualifiers: Quick Peek

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

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 (seeExample 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.

Click for source code

More on CDI

Thanks for reading! :-)

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}