Java Reference
In-Depth Information
Qualifiers are standard Java annotations. Typically, they have retention of runtime
and can target methods, fields, parameters, or types. The only difference between
a qualifier and a standard annotation is that qualifiers are decorated with the
@Qualifier annotation.
Once we have our qualifier in place, we need to use it to decorate the specific
subclass or interface implementation, as shown in the following code:
package com.ensode.cdiintro.model;
import com.ensode.cdiintro.qualifier.Premium;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named
@RequestScoped
@Premium
public class PremiumCustomer extends Customer {
private Integer discountCode;
public Integer getDiscountCode() {
return discountCode;
}
public void setDiscountCode(Integer discountCode) {
this.discountCode = discountCode;
}
}
Once we have decorated the specific instance we need to qualify, we can use our
qualifiers in the client code to specify the exact type of dependency we need:
package com.ensode.cdiintro.controller;
import com.ensode.cdiintro.model.Customer;
import com.ensode.cdiintro.model.PremiumCustomer;
import com.ensode.cdiintro.qualifier.Premium;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
 
Search WWH ::




Custom Search