使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
来源:互联网 发布:在a标签传随机参数js 编辑:程序博客网 时间:2024/06/07 07:15
引言:使用 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可接受一个或者多个参数,例如:
@Profile({"tut1","hello-world"})@Configurationpublic 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 注解取反
请看下面代码:
@Componentpublic 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.profiles.active 及 @profile 注解 动态化配置内部及外部配置
- springboot 使用spring.profiles.active来分区配置
- CXF的Spring注解配置及使用
- CXF的Spring注解配置及使用
- JAVA获取内部及外部配置
- java编程中spring boot 使用spring.profiles.active来分区配置
- spring.profiles.active 针对多种启动环境的spring配置
- spring.profiles.active配置多种spring启动环境
- spring.profiles.active 针对多种启动环境的spring配置
- spring.profiles.active 针对多种启动环境的spring配置
- spring Bean基本配置及注解
- spring-mvc数据源配置及modelAttribute注解
- spring的配置及ioc注解总结
- Spring定时器配置及使用
- spring aop 配置及使用
- Spring事务管理-配置及使用
- log4Net使用及配置注解(整理)
- spring+ibatis【基本配置及动态sql】
- Udacity机器学习入门笔记——Random Forest
- Analyze查找静态内存泄漏
- 读Linux内核源码情景分析(一)
- HaoSQL_好用的SQL等数据库一键包发布!
- Python命令行注册pypi出现410报错无法登陆的问题解决
- 使用 spring.profiles.active 及 @profile 注解 动态化配置内部及外部配置
- hbase存储大数据量相关配置调优
- PAT甲级 1055. The World's Richest (25)
- Greendao 简单实现增删改查使用过GreenDao的同学都知道,3.0之前需要通过新建GreenDaoGenerator工程生成Java数据对象(实体)和DAO对象,非常的繁琐而且也加大了使用成
- java桃心形状
- 5.1 Hadoop数据序列化
- VMWare VMNet 8 的配置使用
- Android 源码系列之<十七>自定义Gradle Plugin,优雅的解决第三方Jar包中的bug<上>
- 多元线性回归