Skip to content

Instantly share code, notes, and snippets.

@K0NRAD
Created February 12, 2014 18:20
Show Gist options
  • Save K0NRAD/8961480 to your computer and use it in GitHub Desktop.
Save K0NRAD/8961480 to your computer and use it in GitHub Desktop.
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
public enum EProductivityPlanType {
TARGET, ACTUAL
}
public interface IProductivityPlan {
void calculate();
}
@ProductivityPlanType(type = EProductivityPlanType.ACTUAL)
public class ProductivityPlanActual implements IProductivityPlan {
@Override
public void calculate() {
// calculate actual productivity plan
}
}
@ProductivityPlanType(type = EProductivityPlanType.TARGET)
public class ProductivityPlanTarget implements IProductivityPlan {
@Override
public void calculate() {
// calculate target productivity plan
}
}
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface ProductivityPlanType {
EProductivityPlanType type() default EProductivityPlanType.TARGET;
}
...
@Inject
@ProductivityPlanType(type = EProductivityPlanType.TARGET)
private IProductivityPlan productivityPlanTarget;
@Inject
@ProductivityPlanType(type = EProductivityPlanType.ACTUAL)
private IProductivityPlan productivityPlanActual;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment