Home » Java » Enterprise Java » Native CDI Qualifiers: @Any and @Default

About Abhishek Gupta

Abhishek Gupta

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!

Reference: Native CDI Qualifiers: @Any and @Default from our JCG partner Abhishek Gupta at the Object Oriented.. blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments