首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多个CDI配置配置文件(开发,beta,qa,生产)在一场战争中?

多个CDI配置配置文件(开发,beta,qa,生产)在一场战争中?
EN

Stack Overflow用户
提问于 2013-06-03 22:57:50
回答 3查看 4.5K关注 0票数 12

在体验了Spring applicationContext.xml声明依赖注入的方法之后,我现在尝试弄清楚如何使用EE6 CDI做同样的事情。

使用Spring,我可以将.jar附带几个配置配置文件,如unittest.xml、devel.xml、qa.xml、production.xml,并使用命令行参数或环境变量激活它们。

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

我不想使用Maven配置文件/过滤器来生成我的应用程序的4-6版本,尽管我理解对于某些场景来说,这将是更好的解决方案(即向客户发送现成的构建wars -但我只在内部使用wars,所以让我们节省编译时间!)

最好,我还可以从文件系统加载这些配置文件,这样系统管理员就可以编辑这些配置文件,而不必重新构建应用程序。

拥有多个依赖项和属性的配置集的JavaEE6方法是什么?

如果没有,那么截至2013年,建议的替代办法是什么?使用Spring?Seam?盖斯?我看到了Apache DeltaSpike的一些内容,但从网页上看,它们仍然是alpha型的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-06-04 15:00:24

我使用动态生成器,使用Qualifier来标识所需的环境

代码语言:javascript
复制
// 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<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; }
     }

}

因此,在这里,容器将所有可用的Greeting实现注入到GreetingFactory中,而后者又作为预期实现的@Producer,根据系统属性‘部署one’做出决定。

票数 12
EN

Stack Overflow用户

发布于 2013-06-04 15:39:58

卡洛的上述答案是好的,我们在DeltaSpike 与ProjectStage中有所有这些。值得一看,这样你就不用自己写了。

票数 2
EN

Stack Overflow用户

发布于 2015-07-03 15:37:20

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

代码语言:javascript
复制
@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()的类型,如果使用概要文件进行注释,而概要文件不活动。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16907185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档