一场战争中有多个CDI配置文件(开发,测试版,qa,生产)?

有了使用Spring DI applicationContext.xml声明dependency injection的方法的经验,我现在试着弄清楚如何用Java EE6 CDI做同样的事情。

使用Spring,我可以将我的.jar与几个配置文件一起发送,例如unittest.xml,devel.xml,qa.xml,production.xml ,并使用命令行参数或环境变量激活它们。

使用CDI,我可以在beans.xml中使用@Alternative和web.xml中的属性,但似乎没有办法为不同的环境提供多个beans.xml。

我不想使用Maven配置文件/filter来生成我的应用程序的4-6版本,虽然我知道在某些情况下这将是更好的解决方案(即向客户发送准备好的构建战争 – 但我只在内部使用我的战争所以让我们节省编译时间!)

优选地,我还能够从文件系统加载这些配置文件,以便系统管理员可以编辑它们而无需重新构建应用程序。

具有多个依赖关系和属性配置集的Java EE6方法是什么?

如果没有,那么2013年的推荐替代品是什么? 用Spring? 接缝? 吉斯? 我看到了Apache DeltaSpike的提及,但它们仍然看起来像网页上的alpha。

我使用动态生产者,使用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 service public 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 code wich uses the service @Stateless public class Salutation{ @Inject Greeting greeting; public String sayHello(){ return greeting.sayHello(); }; } // The dynamic producer public class GreetingFactory{ @Inject @Any Instance greetings; public String getEnvironment(){ return System.getProperty("deployenv"); } @Produces public Greeting getGreeting(){ Instance found=greetings.select( new StageQualifier(getEnvironment())); if (!found.isUnsatisfied() && !found.isAmbiguous()){ return found.get(); } throw new RuntimeException("Error ..."); } public static class StageQualifier extends AnnotationLiteral implements Stage { private String value; public StageQualifier(String value){ this.value=value; } public String value() { return value; } } } 

所以这里容器将所有可用的Greeting实现注入到GreetingFactory ,而GreetingFactory又作为预期的一个用于@Producer ,基于系统属性’deployenv’的决定。

Carlo的上述答案很好,我们在DeltaSpike中使用ProjectStage完成所有这些操作。 值得一看,所以你不必自己写。

M.-Leander Reimer在他的演示文稿中使用CDI扩展建议将一个基于JSF的Web应用程序从Spring 3迁移到Java EE 7和CDI (幻灯片32),提出了另一种解决方案:

 @Alternative @Stereotype @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface ProfileAlternative { Profile[] value(); } public void processAnnotated(@Observes ProcessAnnotatedType event) { ProfileAlternative pa = getProfileAlternative(event); if (profileAlternativeIsNotActive(pa)) { event.veto(); } } 

他使用自定义注释@ProfileAlternative模仿Spring的@Profile和一个CDI扩展,观察ProcessAnnotatedType事件,如果用一个配置文件注释并且配置文件不活动,则将其转换为veto()类型。