首页 > 技术文章 > Springboot:配置文件位置以及多环境配置(六)

applesnt 2020-04-10 22:53 原文

配置文件位置

Springboot配置文件可以加载以下四个位置:

file:./config/     #第一加载位置
file:./            #第二加载位置
classpath:/config/ #第三加载位置
classpath:/        #第四加载位置

多环境配置(多配置文件)

方式一(多配置文件)

格式:application-xxx.yml

application-dev.yml  #开发环境

application-prod.yml #生产环境

application-test.yml #测试环境

只需要在application.yml默认的配置文件配置使用的环境即可切换配置文件

spring:
  profiles:
    active: prod #当前使用生产环境的配置
方式二(多文档块)

在application.yml中用三个横杆进行不同配置环境的定义

#选择何种环境生效
spring:
  profiles:
    active: dev

#开发环境
---
server:
  port: 8081
spring:
  profiles: dev

#测试环境
---
server:
  port: 8082
spring:
  profiles: test

#生产环境
---
server:
  port: 8083
spring:
  profiles: prod

推荐阅读