Created
September 27, 2015 10:07
-
-
Save abhirockzz/1791a06cdc16b9818b14 to your computer and use it in GitHub Desktop.
@Any in action: choosing implementations dynamically
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{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment