Preloader image


Sometimes we need write several implementation for a interface of rule business, to increase uncoupling let’s inject only interface and CDI choose the right implementation, to help CDI with this choice was created the Qualifiers.


In this example, we have an interface Payment and theirs implementations: * Cash * CreditCard

In our test (Payment Test) we inject only the interface Payment, without the Qualifier feature the CDI would not known which implementation to inject in test.

We created a Qualifier called PaymentQualifier a single qualifier with only one difference, the annotation @Qualifier.

@Target({ TYPE, FIELD, METHOD })
public @interface PaymentQualifier {

	PaymentType type();

This qualifier has a method named type(), this method will help the CDI to inject correctly implementation. see this enum:

public enum PaymentType {


now see an implementation

public class Cash implements Payment {

	public String pay() {

		return "cash";

Each implementation should marked with this qualifier.

How to inject? see simplicity

public class PaymentTest {

    private static EJBContainer container;

    @PaymentQualifier(type=PaymentType.CREDITCARD) //qualifier informing the CDI about the correctly implementation
    private Payment paymentCreditCard;

    @PaymentQualifier(type=PaymentType.CASH) //qualifier informing the CDI about the correctly implementation
    private Payment paymentCash;

    public static void start() {
        container = EJBContainer.createEJBContainer();

    public void setUp() throws Exception {
        container.getContext().bind("inject", this);

    public void mustReturnCreditCard() {

        assertEquals(, "creditCard");

    public void mustReturnCash() {

        assertEquals(, "cash");

    public static void stop() {