美文网首页SpringBoot云课堂
Spring中的@Profile与@ActiveProfile

Spring中的@Profile与@ActiveProfile

作者: Tommmmm | 来源:发表于2018-02-12 15:17 被阅读14次

    1、使用@Profile

    配置类

    @Configuration
    public class ProfileConfig {
        @Bean
        @Profile("upper")
        public UpperAction upperAction1(){
            return  new UpperAction("Tom");
        }
    
        @Bean
        @Profile("upper1")
        public UpperAction upperAction2(){
            return  new UpperAction("Jack");
        }
    
        @Bean
        @Profile("lower")
        public LowerAction lowerAction1(){
            return new LowerAction();
        }
    /*
        @Bean
        @Profile("upper0")
        public Action action(){
    
            Action action1 =(Action) new UpperAction("Tom");
    
            return action1;
        }
    */
    }
    

    测试函数

        @Test
        public void test0(){
            AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext();
            ctx.getEnvironment().setActiveProfiles("upper");
            ctx.register(ProfileConfig.class);
            ctx.refresh();
    
            UpperAction upperAction=ctx.getBean(UpperAction.class);
            upperAction.execute("hahaha");
        }
    

    注意:要先将活动的Profile设置为Upper,然后再注册Bean的配置类,不然会报未定义的错误ctx.refresh(),刷新容器,必不可少。

    测试结果 测试结果

    实验中想到的问题1:@Profile的作用是什么?

    图1
    作用:怎么让Spring容器在不同的条件下注册不同的Bean。

    实验中想到的问题2:@Profile与@Qualifier的区别是什么?
    @Qualifier:可以帮助区分相同类型的对象不同使用方法。
    一开始觉得@Profile跟@Service(……)/@Bean(……)效果差不多,都是取了个名字

    但@Profile其实还可以这么用

    @Profile还可以是这样的
    效果是这样的
    image.png
    为什么使用@Profile?
    由于我们平时在开发中,通常会出现在开发的时候使用一个开发数据库,测试的时候使用一个测试的数据库,而实际部署的时候需要一个数据库。以前的做法是将这些信息写在一个配置文件中,当我把代码部署到测试的环境中,将配置文件改成测试环境;当测试完成,项目需要部署到现在了,又要将配置信息改成现在的。非常麻烦。
    而使用了Profile之后,我们就可以分别定义3个配置文件,一个用于开发、一个用户测试、一个用户生产,其分别对应于3个Profile。当在实际运行的时候,只需给定一个参数来激活对应的Profile即可,那么容器就会只加载激活后的配置文件,这样就可以大大省去我们修改配置信息而带来的烦恼。

    2、使用@ActiveProfile

    配置类

    @Configuration
    public class ProfileConfig {
    
        @Bean
        @Profile("upper")
        public UpperAction upperAction1(){
            return  new UpperAction("Tom");
        }
    
        @Bean
        @Profile("upper1")
        public UpperAction upperAction2(){
            return  new UpperAction("Jack");
        }
    
        @Bean
        @Profile("lower")
        public LowerAction lowerAction1(){
            return new LowerAction();
        }
    /*
        @Bean
        @Profile("upper0")
        public Action action(){
            Action action1 =(Action) new UpperAction("Tom");
            return action1;
        }
    */
    }
    

    测试代码

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes =ProfileConfig.class)
    @ActiveProfiles("upper")
    public class Test4_UsingProfile {
    
        @Autowired
        Action action;
    
        @Test
        public void test3() {
            action.execute("wushuohanaaaaaa");
        }
    }
    

    测试结果

    测试结果
    实验中想到的问题1:如果在测试类中不使用@ActiveProfile
    配置类中的 LowerAction1()没有设置@Profile
    @Configuration
    public class ProfileConfig {
    
        @Bean
        @Profile("upper")
        public UpperAction upperAction1(){
            return  new UpperAction("Tom");
        }
    
        @Bean
        @Profile("upper1")
        public UpperAction upperAction2(){
            return  new UpperAction("Jack");
        }
    
        @Bean
        public LowerAction lowerAction1(){
            return new LowerAction();
        }
    }
    

    测试代码

       @Autowired
        LowerAction lowerAction;
    
        @Test
        public void test2() {
            lowerAction.execute("wushuohan");
        }
    

    测试结果

    测试结果
    结论:在不使用@ActiveProfile的时候,profile=default和没有设定profile的Bean会被加载到。这里没有设定Profile的LowerAction被加载到了。
    @Bean把实例化的对象转化成一个Bean,放在容器中。而当Spring 容器启动时,会将扫描 Spring 容器中所有 Bean,当发现有@Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
    所以不管有没有@ActiveProfile都可以加载到没有@Profile的Bean。

    相关文章

      网友评论

        本文标题:Spring中的@Profile与@ActiveProfile

        本文链接:https://www.haomeiwen.com/subject/vsnrtftx.html