Native CDI Qualifiers: @Any and @Default

Let’s take a look at the out-of-the-box qualifiers in CDI

There are three qualifiers declared by the CDI specification – @Any, @Default, @New

  • @Any: Think of it as an omnipresent qualifier. It’s there even if its not 😉

  • @Default: As the name suggests, this qualifier treated as a default when none other qualifiers have been specific. The only exception to this rule is when the @Named (javax.inject) qualifier is used as well

  • @New: Used to obtain a new instance of a bean on-demand. The new instance is scope independent. This has been deprecated since CDI 1.1

Here are some simple examples

Qualifiers at Bean (class) level

Qualifiers at Injection point

What’s so special about @Any ?

As stated earlier, the @Any qualifier is omnipresent i.e. it is always there, no matter what. The interesting part is that if you explicitly mention this annotation, it opens up the following options

  • You have access to all possible implementations of a bean

  • It does not suppress the default bean (if any) or any of the explicit (qualified) implementations. You can still look them up dynamically (at run time)

That’s all for a quickie on default CDI qualifiers. You might want to check out one of my earlier posts on basics of custom qualifiers in CDI

Cheers!

Advertisements

About Abhishek

Java EE & distributed systems junkie who frequently blogs at abhirockzz.wordpress.com as well as simplydistributed.wordpress.com. Oh, I have also authored a few (mini) books, articles, Refcards etc. :-)
This entry was posted in Java, Java EE and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s