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 24, born in Romania, studied 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.

Wednesday, April 13, 2011

Define a CDI Qualifier


I suppose 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.

1 comment:

Cheap Tennis Rackets said...

I like this article, Wilson Tennis Racketsgive us a better life. Meanwhile, also Wilson Tennis Rackets welcome your return visit.