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


//Explicit qualifier not specified, hence @Default is assumed
public class CSVParser implements Parser{
//implementation …
}
//Explicit qualifier specified
@XMLParser
public class XMLParser implements Parser{
//implementation …
}

Qualifiers at Injection point


public class ParsingService{
@Inject
Parser parser //an implementation of the CSVParser class is injected (default behavior)
//business logic
}
public class ParsingService{
@Inject
@XMLParser
Parser parser //an implementation of the XMLParser class is injected
//business logic
}

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


public class ParsingService{
@Inject
@Any
Instance<Parser> parsers; //javax.enterprise.inject.Instance handles the magic behind the scenes
public void availableParsingOptions(){
for(Parser parser : parsers){
System.out.println("Parser implementation class: "+ parser.getConcreteImplName());
System.out.println("Supported format: "+ parser.getSupportedFormat().getName());
}
}
}

  • 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)


public class ParsingService{
@Inject
@Any
Instance<Parser> parsers;
public Parser chooseParser(){
//@XMLParser (qualifier annotation) being enforced programmatically
Parser selected = parsers.select(new XMLParserQualifier()).get();
return (selected.isUnsatisfied() || selected.isAmbiguous()) ? null : selected;
}
//javax.enterprise.util.AnnotationLiteral project qualifiers as objects
private static class XMLParserQualifier extends AnnotationLiteral<XMLParser> implements XMLParser{}
}

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!

Advertisement

About Abhishek

Loves Go, NoSQL DBs and messaging systems
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 )

Facebook photo

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

Connecting to %s