栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在一次战争中有多个CDI配置配置文件(开发,测试版,质量保证,生产)?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在一次战争中有多个CDI配置配置文件(开发,测试版,质量保证,生产)?

我会使用动态生产者,使用

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
预期的实现。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号