springboot中多个配置文件,指定让配置文件生效,以便于达到在开发环境,测试环境,线上环境根据不一样的配置灵活应用。 springboot框架内部提供了2种方式用于加载YAML文档,以供启动时读取配置文件;YamlPropertiesFactoryBean 会加载 YAML变为Properties;YamlMapFactoryBean会转换YAML 为map; springboot中多环境配置文件名必须知足: application-{profile}.properties或application-{profile}.yml固定格式
一:在开发软件IDEA或Eclipse中配置虚拟机参数启动不一样的配置文件如: -Dspring.profiles.active=dev
二:使用Jar包运行时指定不一样的配置文件 如:java -jar xxx.jar --Dspring.profiles.active=devjava
三:新建三不一样环境的yml配置文件
application.yml [application.yml中配置通用内容,并设置spring.profiles.active=dev,以开发环境为默认配置]
配置内容:
spring:
profiles:
active:dev
application-dev.yml
配置内容:
server:
port:8081
application-test.yml
配置内容:
server:
port:8082
application-prod.yml
配置内容:
server:
port:8083spring
在application.yml 引入其它配置文件。springboot
application.yml
spring:
##引入外部配置文件
profiles:
include:pay,test
application-pay.yml
pay:
attr:application-pay.yml
application-test.yml
test:
attr:application-test.ymlapp