使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置

https://blog.csdn.net/swordsnapliu/article/details/78540902

引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发、测试、生产)配置参数的切换

一.根据springboot的配置文件命名约定,结合active可在不同环境引用不同的properties外部配置

参考官方文档:
springboot官方文档截图
根据文档描述,我们除application.properties外,还可以根据命名约定(
命名格式:application-{profile}.properties)来配置,如果active赋予的参数没有与使用该命名约定格式文件相匹配的话,app则会默认从名为application-default.properties 的配置文件加载配置。
如:spring.profiles.active=hello-world,sender,dev 有三个参数,其中 dev 正好匹配下面配置中的application-dev.properties 配置文件,所以app启动时,项目会先从application-dev.properties加载配置,再从application.properties配置文件加载配置,如果有重复的配置,则会以application.properties的配置为准。(配置文件加载顺序详见官方文档:24. Externalized Configuration)
springboot外部配置
如此,我们就不用为了不同的运行环境而去更改大量的环境配置了(此处,dev、pro、test分别为:开发、生产、测试环境配置)

二.通过@Profile注解匹配active参数,动态加载内部配置
参考官方文档:
springboot官方文档截图

1.@Profile注解使用范围:@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…
2.@Profile可接受一个或者多个参数,例如:

@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {
    @Bean
    publicQueuehello() {
        return newQueue("hello");
    }
    @Profile("receiver")
    @Bean
    publicTut1Receiverreceiver() {
        return newTut1Receiver();
    }
    @Profile("sender")
    @Bean
    publicTut1Sendersender() {
        return newTut1Sender();
    }
}

当 spring.profiles.active=hello-world,sender 时,该配置类生效,且第一个@Bean和第三个@Bean生效
如果spring.profiles.active=hello-world ,则该配置文件生效,第一个@Bean生效
如果spring.profiles.active=sender ,该配置文件未生效,所以下面的@Bean都不会生效
如此,当我们的项目需要运行在不同环境,特异化配置又比较多,该注解的优势是相当明显的!

三.@Profile 注解取反
请看下面代码:

@Component
public class Tut1CommindLineRunner {
    @Profile("usage_message")
    @Bean
    publicCommandLineRunnerusage() {
        return strings -> {
            System.out.println("This app uses Spring Profiles to control its behavior.\n");
            System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
        };
    }
    @Profile("!usage_message")
    @Bean
    publicCommandLineRunnertutorial() {
        return newRabbitAmqpTutorialsRunner();
    }
}

该@Profile注解接收了两个类似于取反一样的配置参数,该用法我没有找到相关的官方文档,这是在rabbitmq官网,关于springboot amqp 的一段代码。
通过查看源代码,我发现,其实这个就是一个类似于取反的意思,这里理解为:匹配不上该配置参数的时候,启用该配置,相关源代码:

package org.springframework.core.env;
//...
public abstract classAbstractEnvironmentimplementsConfigurableEnvironment {
    //...
    public booleanacceptsProfiles(String... profiles) {
        Assert.notEmpty(profiles,"Must specify at least one profile");
        String[] var2 = profiles;
        int var3 = profiles.length;
        for(intvar4 =0;var4 < var3;++var4) {
            String profile = var2[var4];
            if(StringUtils.hasLength(profile) && profile.charAt(0) == 33) {
                if(!this.isProfileActive(profile.substring(1))) {
                    return true;
                }
            } else if(this.isProfileActive(profile)) {
                return true;
            }
        }
        return false;
    }

    protected booleanisProfileActive(String profile) {
        this.validateProfile(profile);
        Set<String> currentActiveProfiles =this.doGetActiveProfiles();
        return currentActiveProfiles.contains(profile) || currentActiveProfiles.isEmpty() &&this.doGetDefaultProfiles().contains(profile);
    }
    //...
}

“!”对应的char 编码为:33
此处:StringUtils.hasLength(profile) && profile.charAt(0) == 33 ,如果配置以 “!”开头,则将“!”后的字符串与配置进行匹配,并取反,如果为true,则返回true,即active没有配置该配置参数,则返回true,即启动该配置。

如有理解错误或者描述不正确的地方,欢迎留言!

参考:
http://blog.csdn.net/he90227/article/details/52981747 不错的博客
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/htmlsingle/ 官方文档

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值