spring boot之多环境配置

来源:互联网 发布:大数据r语言 编辑:程序博客网 时间:2024/05/18 01:08

一.多环境配置的好处:
1.不同环境配置可以配置不同的参数
2.便于部署,提高效率,减少出错

二.Properties多环境配置
1. application.properties中配置激活选项
spring.profiles.active=dev
2.添加其他配置文件
application-dev.properties—》server.port=8080
application-test.properties—》server.port=8090
application-prod.properties—》server.port=80

三.application.yaml多环境配置
配置文件环境配置
spring:
profiles:
active: dev
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing


spring:
profiles: dev
server:
port: 8080


spring:
profiles: test
server:
port: 8090


spring:
profiles: prod
server:
port: 80

四.两种配置方式的比较
1.Properties配置多环境,需要添加多个配置文件,YAML只需要一个配件文件
2.书写格式的差异,yaml相对比较简洁,优雅
3.YAML的缺点:不能通过@PropertySource注解加载。如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。
五.如何使用
java -jar myapp.jar –spring.profiles.active=dev

原创粉丝点击