Spring中的@Profile与@ActiveProfile

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。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值