Over a million developers have joined DZone.

The built-in qualifiers @Default and @Any

DZone's Guide to

The built-in qualifiers @Default and @Any

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

• @Default - Whenever a bean or injection point does not explicitly declare a qualifier, the container assumes the qualifier @Default.

• @Any – When you need to declare an injection point without specifying a qualifier, is useful to know that all beans have the @Any qualifier. This qualifier “belongs” to all beans and injection points (not applicable when @New is present) and when is explicitly specified you suppress the @Default qualifier. This is useful if you want to iterate over all beans with a certain bean type.

The example presented in this post will prove how @Any qualifier works in a useful example. You will try to iterate over all beans with a certain bean type. For start, you define a new Java type, like below – a simple interface representing a Wilson tennis racquet type:

package com.racquets;

public interface WilsonType {
public String getWilsonRacquetType();

Now, three Wilson racquets will implement this interface – notice that no qualifier was specified:
package com.racquets;

public class Wilson339Racquet implements WilsonType{

public String getWilsonRacquetType() {
return ("Wilson BLX Six.One Tour 90 (339 gr)");


package com.racquets;

public class Wilson319Racquet implements WilsonType {

public String getWilsonRacquetType() {
return ("Wilson BLX Six.One Tour 90 (319 gr)");

package com.racquets;

public class Wilson96Racquet implements WilsonType {

public String getWilsonRacquetType() {
return ("Wilson BLX Pro Tour 96");

Next, you can use the @Any qualifier at injection point to iterate over all beans of type WilsonRacquet. An instance of each implementation is injected and the result of getWilsonRacquetType method is stored in an ArrayList:
package com.racquets;

import java.util.ArrayList;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

public class WilsonRacquetBean {

private @Named @Produces ArrayList wilsons = new ArrayList();

void initWilsonRacquets(@Any Instance racquets) {
for (WilsonType racquet : racquets) {

From a JSF page, you can easily iterate over the ArrayList (notice here that the wilsons collection was annotated with @Named (allows JSF to have access to this field) and @Produces (as a simple explanation, a producer field suppress the getter method)):
<br/><h3><b>The built-in qualifiers @Default and @Any</b></h3>
<h:dataTable value="#{wilsons}" var="item" border="1">
<f:facet name="header" >
<h:outputText value="Racquets"/>
<h:outputText value="#{item}"/>

The output is in figure below:


From http://e-blog-java.blogspot.com/2011/04/built-in-qualifiers-default-and-any.html

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}