Jave EE usage of @Qualifier #- create type enum #- create qualifier annotation #- create service interface #- create implementations of service interface #- inject and use qualifier annotation to specify the correct service
...
@Inject
@ProductivityPlanType(type = EProductivityPlanType.TARGET)
private IProductivityPlan productivityPlanTarget;
@Inject
@ProductivityPlanType(type = EProductivityPlanType.ACTUAL)
private IProductivityPlan productivityPlanActual;
...
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface ProductivityPlanType {
EProductivityPlanType type() default EProductivityPlanType.TARGET;
}
@ProductivityPlanType(type = EProductivityPlanType.TARGET)
public class ProductivityPlanTarget implements IProductivityPlan {
@Override
public void calculate() {
// calculate target productivity plan
}
}
@ProductivityPlanType(type = EProductivityPlanType.ACTUAL)
public class ProductivityPlanActual implements IProductivityPlan {
@Override
public void calculate() {
// calculate actual productivity plan
}
}
public interface IProductivityPlan {
void calculate();
}
public enum EProductivityPlanType {
TARGET, ACTUAL
}