我会使用动态生产者,使用
Qualifier来标识所需的环境
// The qualifier for the production/qa/unit test @Qualifier@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})public @interface Stage { String value() default "production";}// The interface for the stage-dependant servicepublic interface Greeting{ public String sayHello();}// The production service@Stage("production")public class ProductionGreeting implements Greeting{ public String sayHello(){return "Hello customer"; }}// The QA service@Stage("qa")public class QAGreeting implements Greeting{ public String sayHello(){return "Hello tester"; }}// The common pre wich uses the service@Statelesspublic class Salutation{ @Inject Greeting greeting; public String sayHello(){ return greeting.sayHello(); };}// The dynamic producerpublic class GreetingFactory{ @Inject @Any Instance<Greeting> greetings; public String getEnvironment(){ return System.getProperty("deployenv"); } @Produces public Greeting getGreeting(){ Instance<Greeting> found=greetings.select(new StageQualifier(getEnvironment())); if (!found.isUnsatisfied() && !found.isAmbiguous()){return found.get(); } throw new RuntimeException("Error ..."); } public static class StageQualifier extends AnnotationLiteral<Stage> implements Stage { private String value; public StageQualifier(String value){this.value=value; } public String value() { return value; } }}因此,此处容器根据系统属性“
deployenv”的决定将所有可用的
Greeting实现注入到中
GreetingFactory,而后者又用作
@Producer预期的实现。



