Spring Boot Learning(配置文件--多環境配置)


多環境配置的好處:

1.不同環境配置可以配置不同的參數
2.便於部署,提高效率,減少出錯

 

Properties多環境配置

1. 配置激活選項
    spring.profiles.active=dev
2.添加其他配置文件

   application-test.properties

   application-dev.properties

   application-prod.properties

   

Yaml多環境配置:

1.配置激活選項
  spring:
    profiles:
      active: dev
2.在配置文件添加三個英文狀態下的短橫線即可區分
---
  spring:
    profiles: dev

兩種配置文件的比較:

1. Properties配置多環境,需要添加多個配置文件,YAML只需要一個配件文件
2.書寫格式的差異,yaml相對比較簡潔,優雅
3. YAML的缺點:不能通過@PropertySource注解加載。如果需要使用@PropertySource注解的方式加載值,那就要使用properties文件。

 

打包后可以通過命令行參數的方式指定配置文件:java -jar myapp.jar --spring.profiles.active=dev

代碼:

 

application.properties:

spring.profiles.active=dev

server.port=8080
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing

application-test.properties:

#如果主application.properties有相同屬性,那么這個會覆蓋主properties里面的參數
server.port=8081
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing

application-dev.properties:

server.port=8080
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing

application-prod.properties:

server.port=8082
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing

 

Yaml方式:

 

application.yaml:

server:
  port: 8090
  
spring:
  profiles:
    active: prod
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: Asia/Chongqing
 
---
spring:
  profiles: dev

server:
  port: 8080
  
---
spring:
  profiles: test

server:
  port: 8081
 
---
spring:
  profiles: prod

server:
  port: 8082

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM