SpringBoot 使用(三): 設定檔詳解

來源:互聯網
上載者:User

標籤:iii   config   value   set   select   god   ***   ica   排列   

代碼從開發到測試要經過各種環境,開發環境,測試環境,demo環境,線上環境,各種環境的配置都不一樣,同時要方便各種角色如營運,介面測試, 功能測試,全鏈路測試的配置,hardcode 肯定不合適,如Spring profile一樣寫在jar包不合適.分布式應用中一般採用集中管理配置的方式,通過使用開源軟體,如阿里的diamond,hashicorp的consul等等,SpringBoot中提供了各種各樣的配置方式,下面一一來分析下.

1.配置方式和優先順序

這些方式優先順序如下:
a. 命令列參數
b. 來自java:comp/env的JNDI屬性
b. Java系統屬性(System.getProperties())
d. 作業系統環境變數
e. RandomValuePropertySource配置的random.*屬性值
f. jar外部的application-{profile}.properties或application.yml(帶spring.profile)設定檔
g. jar內部的application-{profile}.properties或application.yml(帶spring.profile)設定檔
h. jar外部的application.properties或application.yml(不帶spring.profile)設定檔
i. jar內部的application.properties或application.yml(不帶spring.profile)設定檔
j. @Configuration註解類上的@PropertySource
k. 通過SpringApplication.setDefaultProperties指定的預設屬性

2.命令列參數
@Componentpublic class UserProperties {      @Value("${myname}")      private  String myname;}

啟動命令: java -jar *.jar --myname="KevinSun"
其他java 系統屬性, 作業系統屬性都是類似處理方案如果不想通過命令列啟動 可以用下面方法取消.

SpringApplication.setAddCommandLineProperties(false);
3. application-{profile}.properties參數載入

不同的環境可能需要不同配置,可以通過
application{profile}.properties來解決這個問題.
首先:建立application-dev..properties 檔案,增加需要參數其次啟動命令:

  java -jar  ***.jar  --spring.profiles.active=dev

application-{profile}.properties 檔案和預設的application.properties的載入方式本質是一致的,可以參照4中的內容.需要注意的是:application.properties 會首先被載入.

然後:從application-{profile}.properties中擷取替換,所以一些基本固定的值可以寫在application.properties, 然後個人化配置寫在application-{profile}.properties中。

4.application.properties 參數載入

SpringBoot中預設的從application.properties檔案中載入參數,大量情況下預設是寫在這個檔案中的.
a. application.properties 讀取的優先順序
i. file:./config/ 當前jar目錄的config
ii. file:./ 當前jar目錄
iii. classpath:/config/ jar包中classpath的 config目錄下
iv. classpath:/ jar包中classpath 路徑下
排列的順序 就是 載入的優先順序,application.properties只會被載入一次
b. 使用自訂檔案
如果你覺得 application.properties不夠酷,你可以定義自己的檔案名稱, 這裡也有兩種方式
i. 使用命令

  java -jar  ***.jar --spring.config.name=myproject

這時就會載入myproject.propertie並且 application.properties不會被載入的.
ii. 使用命令

 java -jar  ***.jar  spring.config.location=classpath:/myproject.properties

這種情況下 application.properties也是會被載入,使用方式application-{profile}.properties相同.
c. 使用${}進行屬性預留位置替換

spring.data.mongodb.host=192.168.1.1spring.data.mongodb.port=1234spring.data1.mongodb.host=${spring.data.mongodb.host}_testspring.data1.mongodb.port=${spring.data.mongodb.port}_testspring.data1.mongodb.database=${random.value}_test

注意最後一個配置,這使用random 生產隨機值的方式, 在測試中可以用來造資料。
d. 型別安全載入
使用@value 進行屬性的注入有的時候可能比較笨重, spring 提供一種強型別的bean 來替代這種方式

@Configuration@EnableConfigurationProperties(UserProperties.class)public class MyConfiguration{}
@Component@ConfigurationProperties(prefix = "spring.data.mongodb")public class UserProperties {    private  String host;    private  int prot;}
spring.data.mongodb.host=192.168.1.1spring.data.mongodb.port=1234
5.使用application.yaml 參數載入

YAML文檔也可以比較好的被支援, YamlPropertiesFactoryBean會將yaml檔案當初properties來解析的。

spring:   data.mongodb:     host:192.168.1.1     port:27017

但是YAML檔案不能通過@PropertySource註解載入. 所以,在這種情況下,如果需要使用@PropertySource註解的方式載入值,那就要使用properties檔案.

SpringBoot 使用(三): 設定檔詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.