DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Define a CDI Qualifier

Define a CDI Qualifier

A. Programmer user avatar by
A. Programmer
·
Apr. 14, 11 · Java Zone · Interview
Like (0)
Save
Tweet
17.04K 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

  • Kubernetes Service Types Explained In-Detail
  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • Implementing Microservices Architectures
  • Streaming ETL with Apache Kafka in the Healthcare Industry

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo