Define a CDI Qualifier
Join the DZone community and get the full member experience.
Join For FreeImagine 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;More, let us develop two simple implementations of this interface. You have a Head power racquet:
public interface RacquetType {
public String getRacquetType();
}
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.
Comments