Spring Boot Learning(設定檔--多環境配置)

來源:互聯網
上載者:User

標籤:src   相對   註解   通過   區分   --   asi   color   png   

多環境配置的好處:

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=devserver.port=8080spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.time-zone=Asia/Chongqing

application-test.properties:

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

application-dev.properties:

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

application-prod.properties:

server.port=8082spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.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: devserver:  port: 8080  ---spring:  profiles: testserver:  port: 8081 ---spring:  profiles: prodserver:  port: 8082

 

Spring Boot Learning(設定檔--多環境配置)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.