Spring Boot - Profile配置

Profile是什么
Profile我也找不出合适的中文来定义,简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。
Profile使用
假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。
基于properties文件类型
你可以另外建立3个环境下的配置文件:
applcation.properties
application-dev.properties
application-test.properties
application-prod.properties
然后在applcation.properties文件中指定当前的环境:
spring.profiles.active=test
这时候读取的就是application-test.properties文件。
基于yml文件类型
只需要一个applcation.yml文件就能搞定,推荐此方式。
spring:
profiles:
active: prod
---
spring:
profiles: dev
server:
port: 8080
---
spring:
profiles: test
server:
port: 8081
---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq
server:
port: 8082
---
spring:
profiles: proddb
db:
name: mysql
---
spring:
profiles: prodmq
mq:
address: localhost
此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。
也可以同时激活三个配置。
spring.profiles.active: prod,proddb,prodmq
基于Java代码
在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile注解只能组合使用@Configuration和@Component注解。
@Configuration
@Profile("prod")
public class ProductionConfiguration {
// ...
}
指定Profile
main方法启动方式:
// 在Eclipse Arguments里面添加
--spring.profiles.active=prod
插件启动方式:
spring-boot:run -Drun.profiles=prod
jar运行方式:
java -jar xx.jar --spring.profiles.active=prod
除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles方法。
SpringApplication.class
public void setAdditionalProfiles(String... profiles) {
this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
}
Spring Boot - Profile配置的更多相关文章
- spring boot 环境配置(profile)切换
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- Spring Boot 属性配置和使用(转)
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- 转:spring boot log4j2配置(使用log4j2.yml文件)---YAML 语言教程
转:spring boot log4j2配置(使用log4j2.yml文件) - CSDN博客http://blog.csdn.net/ClementAD/article/details/514988 ...
- Spring Boot 日志配置
Spring Boot 日志配置 默认日志 Logback: 默认情况下,Spring Boot会用Logback来记录日志,并用INFO级别输出到控制台.在运行应用程序和其他例子时,你应该已经看到很 ...
- Spring Boot 属性配置&自定义属性配置
在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...
- Spring Boot自定义配置与加载
Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...
- Spring Boot常用配置
概述 本文主要写了下Spring Boot的一些常用配置. Spring Boot基本配置 入口类: Spring Boot通常有一个名为*Application的入口类,入口类里面有一个main方法 ...
- Spring Boot - Logback配置日志要考虑哪些因素
Spring Boot - Logback配置日志 出于性能等原因,Logback 目前是springboot应用日志的标配: 当然有时候在生产环境中也会考虑和三方中间件采用统一处理方式.@pdai ...
随机推荐
- Django_models下划线__正反查询,对象正反查询
1.我们使用models对数据库进行查询的时候,想去拿到结果的时候有时直接使用".字段",有时是'[0].字段',区别就是我们使用的语句返回的到底是一个对象还是列表: obj=mo ...
- Swagger注解
swagger注解说明 1.与模型相关的注解,用在bean上面 @ApiModel:用在bean上,对模型类做注释: @ApiModelProperty:用在属性上,对属性做注释 2.与接口相关的注 ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- 【Django】关于数据过滤
学到关于数据库过滤方面的内容总结部分注意点: views.py def TestFilter(request): # 多条件过滤 # list=BookInfo.book_manager.filter ...
- animation渐进实现点点点等待效果
<style> @keyframes dot { 0% { width: 0; } 33% { width: .2em; } 66% { width: .5e ...
- django 模板使用
1 配置 在工程中创建模板目录templates. 在settings.py配置文件中修改TEMPLATES配置项的DIRS值: TEMPLATES = [ { 'BACKEND': 'django. ...
- Object强转为实体类类型失败!!!!!!
这是从我CSDN博客直接拿来的图片废话不多说,直接上代码:
- vue案例 - vuex+sessionstorage解决vue项目刷新后页面空白/数据丢失
第一部分 SessionStorage 首先查看sessionStorage的地方在控制台的 Application > Storage > Session Storage这里: 根据se ...
- 四、Vmware虚拟机三种网络模式详解
转载自: http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 1. ...
- cnetos 下 rar 解压
第一步:http://www.rarlab.com/rar/rarlinux-x64-5.3.0.tar.gz 下载 文件 或 wget http://www.rarlab.com/rar/rarli ...