spring boot 讀取設定檔(application.yml)中的屬性值111

來源:互聯網
上載者:User

標籤:collect   ica   highlight   eval   data   dep   string類   bar   支援   

在spring boot中,簡單幾步,讀取設定檔(application.yml)中各種不同類型的屬性值:

1、引入依賴:

[html] view plain copy 
  1. <!-- 支援 @ConfigurationProperties 註解 -->  
  2. <dependency>  
  3.     <groupId>org.springframework.boot</groupId>  
  4.     <artifactId>spring-boot-configuration-processor</artifactId>  
  5.     <optional>true</optional>  
  6. </dependency>  

 

2、設定檔(application.yml)中配置各個屬性的值:

 

[plain] view plain copy 
  1. myProps: #自訂的屬性和值  
  2.   simpleProp: simplePropValue  
  3.   arrayProps: 1,2,3,4,5  
  4.   listProp1:  
  5.     - name: abc  
  6.       value: abcValue  
  7.     - name: efg  
  8.       value: efgValue  
  9.   listProp2:  
  10.     - config2Value1  
  11.     - config2Vavlue2  
  12.   mapProps:  
  13.     key1: value1  
  14.     key2: value2  


3、建立一個bean來接收配置資訊:

 

[java] view plain copy 
  1. @Component  
  2. @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的屬性  
  3. public class MyProps {  
  4.     private String simpleProp;  
  5.     private String[] arrayProps;  
  6.     private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1裡面的屬性值  
  7.     private List<String> listProp2 = new ArrayList<>(); //接收prop2裡面的屬性值  
  8.     private Map<String, String> mapProps = new HashMap<>(); //接收prop1裡面的屬性值  
  9.       
  10.     public String getSimpleProp() {  
  11.         return simpleProp;  
  12.     }  
  13.       
  14.     //String類型的一定需要setter來接收屬性值;maps, collections, 和 arrays 不需要  
  15.     public void setSimpleProp(String simpleProp) {  
  16.         this.simpleProp = simpleProp;  
  17.     }  
  18.       
  19.     public List<Map<String, String>> getListProp1() {  
  20.         return listProp1;  
  21.     }  
  22.     public List<String> getListProp2() {  
  23.         return listProp2;  
  24.     }  
  25.   
  26.     public String[] getArrayProps() {  
  27.         return arrayProps;  
  28.     }  
  29.   
  30.     public void setArrayProps(String[] arrayProps) {  
  31.         this.arrayProps = arrayProps;  
  32.     }  
  33.   
  34.     public Map<String, String> getMapProps() {  
  35.         return mapProps;  
  36.     }  
  37.   
  38.     public void setMapProps(Map<String, String> mapProps) {  
  39.         this.mapProps = mapProps;  
  40.     }  
  41. }  

 

 

 

 

啟動後,這個bean裡面的屬性就會自動接收配置的值了。

4、單元測試用例:

 

[java] view plain copy 
  1. @Autowired  
  2.     private MyProps myProps;   
  3.       
  4.     @Test  
  5.     public void propsTest() throws JsonProcessingException {  
  6.         System.out.println("simpleProp: " + myProps.getSimpleProp());  
  7.         System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));  
  8.         System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));  
  9.         System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));  
  10.         System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));  
  11.     }  



 

測試結果:

 

[plain] view plain copy 
  1. simpleProp: simplePropValue  
  2. arrayProps: ["1","2","3","4","5"]  
  3. listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]  
  4. listProp2: ["config2Value1","config2Vavlue2"]  
  5. mapProps: {"key1":"value1","key2":"value2"}  

原始碼參考:https://github.com/xujijun/my-spring-boot

 

spring boot 讀取設定檔(application.yml)中的屬性值111

相關文章

聯繫我們

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