DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Spring, IoC Containers, and Static Code: Design Principles
  • Understanding Jakarta EE 8 CDI (Part 2): Qualifying Your Beans
  • Understanding Jakarta EE 8 - CDI Part 1

Trending

  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Integrating Security as Code: A Necessity for DevSecOps
  • Unlocking AI Coding Assistants Part 2: Generating Code
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  1. DZone
  2. Coding
  3. Frameworks
  4. The built-in qualifiers @Default and @Any

The built-in qualifiers @Default and @Any

By 
A. Programmer user avatar
A. Programmer
·
Apr. 21, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
8.6K Views

Join the DZone community and get the full member experience.

Join 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{

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

}

package com.racquets;

public class Wilson319Racquet implements WilsonType {

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

package com.racquets;

public class Wilson96Racquet implements WilsonType {

@Override
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;

@RequestScoped
public class WilsonRacquetBean {

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

@Inject
void initWilsonRacquets(@Any Instance racquets) {
for (WilsonType racquet : racquets) {
wilsons.add(racquet.getWilsonRacquetType());
}
}
}


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:form>
<h:dataTable value="#{wilsons}" var="item" border="1">
<h:column>
<f:facet name="header" >
<h:outputText value="Racquets"/>
</f:facet>
<h:outputText value="#{item}"/>
</h:column>
</h:dataTable>
</h:form>



The output is in figure below:

 

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

Bean (software) Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • Functional Endpoints: Alternative to Controllers in WebFlux
  • Spring, IoC Containers, and Static Code: Design Principles
  • Understanding Jakarta EE 8 CDI (Part 2): Qualifying Your Beans
  • Understanding Jakarta EE 8 - CDI Part 1

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!