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.

Click for source code

More on CDI

Thanks for reading! šŸ™‚

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java, Java EE and tagged , , , , . Bookmark the permalink.

3 Responses to Java EE CDI Qualifiers: Quick Peek

  1. Manoj says:

    As you are form IDM domain and i am pretty sure you are good in that too. Can you please post about architecture or how individual components of Oracle Identity and Access Manager work together.

    Also if you can put lights on basic of OAM or OAM-OIM integrationā€¦ it will be great

    Thanks in advance.

    Like

  2. Johna0 says:

    Very good blog post.Really thank you! Fantastic. kdfaedbbecea

    Like

  3. Pingback: Native CDI Qualifiers: @Any and @Default | Thinking in Java (at least trying to!)

Leave a comment