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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!