Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking.I'm 23, born in Romania, student at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff. I know that I look and act differently from most developers, but this is why you will love to work with me! Constantin has posted 42 posts at DZone. You can read more from them at their website. View Full User Profile

Define a CDI Qualifier

04.14.2011
| 5231 views |
  • submit to reddit

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

Published at DZone with permission of its author, Constantin Alin.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Max Rydahl Andersen replied on Thu, 2011/04/14 - 11:14am

Good and simple, btw. JBoss Tools/Developer Studio has a quick fix (accessible via Ctrl+1) that does this stuff for you automatically when you have an ambigious @Inject.

Witold Szczerba replied on Thu, 2011/04/14 - 4:43pm

How to assign qualifiers to implementations without modifying the implementations?

In Guice, that would be something like this:

bind(RacquetType.class).annotatedWith(SpinRacquet.class).to(BabolatRacquet.class); bind(RacquetType.class).annotatedWith(PowerRacquet.class).to(HeadRacquet.class);

Reza Rahman replied on Fri, 2011/04/15 - 4:28pm

You could use CDI XML configuration to add the additional meta-data. Otherwise, you can use the CDI SPI. In most cases, it's esier and simpler to change the implementation.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.