使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
2017-11-15 14:59
696 查看
引言:使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发、测试、生产)配置参数的切换
一.根据springboot的配置文件命名约定,结合active可在不同环境引用不同的properties外部配置
参考官方文档:
根据文档描述,我们除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)
如此,我们就不用为了不同的运行环境而去更改大量的环境配置了(此处,dev、pro、test分别为:开发、生产、测试环境配置)
二.通过@Profile注解匹配active参数,动态加载内部配置
参考官方文档:
1.@Profile注解使用范围:@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…
2.@Profile可接受一个或者多个参数,例如:
当 spring.profiles.active=hello-world,sender 时,该配置类生效,且第一个@Bean和第三个@Bean生效
如果spring.profiles.active=hello-world ,则该配置文件生效,第一个@Bean生效
如果spring.profiles.active=sender ,该配置文件未生效,所以下面的@Bean都不会生效
如此,当我们的项目需要运行在不同环境,特异化配置又比较多,该注解的优势是相当明显的!
三.@Profile 注解取反
请看下面代码:
该@Profile注解接收了两个类似于取反一样的配置参数,该用法我没有找到相关的官方文档,这是在rabbitmq官网,关于springboot amqp 的一段代码。
通过查看源代码,我发现,其实这个就是一个类似于取反的意思,这里理解为:匹配不上该配置参数的时候,启用该配置,相关源代码:
“!”对应的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/ 官方文档
一.根据springboot的配置文件命名约定,结合active可在不同环境引用不同的properties外部配置
参考官方文档:
根据文档描述,我们除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)
如此,我们就不用为了不同的运行环境而去更改大量的环境配置了(此处,dev、pro、test分别为:开发、生产、测试环境配置)
二.通过@Profile注解匹配active参数,动态加载内部配置
参考官方文档:
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/ 官方文档
相关文章推荐
- spring boot 入门 使用spring.profiles.active来分区配置-java编程
- springboot 使用spring.profiles.active来分区配置
- springboot 使用spring.profiles.active 区分不同环境下配置文件
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- java编程中spring boot 使用spring.profiles.active来分区配置
- spring使用注解时配置文件的写法
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- spring aop的使用(注解方式以及基于xml配置方式)
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- SPRING.NET 1.3.2 学习7--使用外部对象和内部对象
- CXF的Spring注解配置及使用
- spring使用注解时配置文件的写法
- JdbcTemplae使用入门&&Spring三种连接池配置&&Spring配置文件引用外部properties文件
- spring 注解配置使用id @Qualifier("flightCacheManager")
- OA中总结:s:select,关于使用modelDriven,项目分层,@Transactional,jspf,各个层上配置注解交给spring管理的方法,简单的OGNL表达式写法
- spring3 混合使用 XML 与注解(Annotation)进行 Bean 的配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 使用注解方式配置spring中的jdbc事务
- 使用Spring 2.5注解自动注册Bean的配置