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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Define a CDI Qualifier

Define a CDI Qualifier

A. Programmer user avatar by
A. Programmer
·
Apr. 14, 11 · Interview
Like (0)
Save
Tweet
Share
17.51K Views

Join the DZone community and get the full member experience.

Join For Free

Imagine a classic scenario – a set of beans that implements the same bean type, and injection points for those beans. Usually, when bean type has multiple implementations, you deal with a Java interface type, like this one – a simple interface representing a tennis racquet type:

package com.racquets;

public interface RacquetType {
public String getRacquetType();
}

More, let us develop two simple implementations of this interface. You have a Head power racquet:
package com.racquets;

public class HeadRacquet implements RacquetType {

@Override
public String getRacquetType() {
return("Head YouTek Speed Pro");
}

}


Moreover, a Babolat spin racquet:
package com.racquets;

public class BabolatRacquet implements RacquetType{

@Override
public String getRacquetType() {
return ("Babolat AeroPro Drive GT");
}

}


At this moment, you can inject these implementations into a bean (@Inject), but an ambiguous injection error will occur, because the container will not know which implementation must be injected. Qualifiers will save us, because they will uniquely identify the implementations, therefore the container will know exactly what to inject. For non-tennis fans, the Head racquet is a power racquet, while the Babolat racquet is a spin racquet, therefore you can use this to define the below two qualifiers:
 package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface PowerRacquet {
}

package com.racquets;

//imports here

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface SpinRacquet {
}


Now, you should annotate the two implementations accordingly, as below – at this point, the container is able to distinguish between the two implementations:
package com.racquets;

@PowerRacquet
public class HeadRacquet implements RacquetType {

@Override
public String getRacquetType() {
return("Head YouTek Speed Pro");
}

}

package com.racquets;

@SpinRacquet
public class BabolatRacquet implements RacquetType{

@Override
public String getRacquetType() {
return ("Babolat AeroPro Drive GT");
}

}


Next, you can give a test to the container, by injecting the two implementations into another bean, like this – if everything works fine, you will not get errors, and you will inject the correct information – this is the prove that the container is capable to distinguish between the Head and Babolat racquets:
package com.racquets;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class RacquetBean {

private String racquetSpin;
private String racquetPower;
private RacquetType racquetSpinType;
private RacquetType racquetPowerType;

@Inject
public RacquetBean(@SpinRacquet RacquetType racquetSpinType,
@PowerRacquet RacquetType racquetPowerType) {
this.racquetSpinType = racquetSpinType;
this.racquetPowerType = racquetPowerType;
}

public String getRacquetPower() {
racquetPower = racquetPowerType.getRacquetType();
return racquetPower;
}

public String getRacquetSpin() {
racquetSpin = racquetSpinType.getRacquetType();
return racquetSpin;
}
}


Call the racquetSpin and racquetPower property from a JSF page through EL (notice that the @Named built-in qualifier let JSF to do this):
<h3><b>Qualifier annotations</b></h3>
Selected Racquets:<br/>
<b>Power Racquet:</b><h:outputText value="#{racquetBean.racquetPower}" /><br/>
<b>Spin Racquet:</b><h:outputText value="#{racquetBean.racquetSpin}" /><br/>


Moreover, the output is in figure below:

Note: Qualifier annotations can also qualify method arguments of producer, disposer and observer methods.

 

From http://e-blog-java.blogspot.com/2011/04/define-cdi-qualifier.html

CDI

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Securing Cloud-Native Applications: Tips and Tricks for Secure Modernization
  • Beyond Coding: The 5 Must-Have Skills to Have If You Want to Become a Senior Programmer
  • Key Elements of Site Reliability Engineering (SRE)
  • How Agile Architecture Spikes Are Used in Shift-Left BDD

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: