1. 程式人生 > >4. 讀取配置檔案(application.yml)中的屬性值

4. 讀取配置檔案(application.yml)中的屬性值

在spring boot中,簡單幾步,讀取配置檔案(application.yml)中各種不同型別的屬性值:

1、引入依賴:
  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)中配置各個屬性的值:

  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來接收配置資訊:
  1. @Component
  2. @ConfigurationProperties(prefix="myProps"//接收application.yml中的myProps下面的屬性
  3. publicclass 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.     public String getSimpleProp() {  
  10.         return simpleProp;  
  11.     }  
  12.     //String型別的一定需要setter來接收屬性值;maps, collections, 和 arrays 不需要
  13.     publicvoid setSimpleProp(String simpleProp) {  
  14.         this.simpleProp = simpleProp;  
  15.     }  
  16.     public List<Map<String, String>> getListProp1() {  
  17.         return listProp1;  
  18.     }  
  19.     public List<String> getListProp2() {  
  20.         return listProp2;  
  21.     }  
  22.     public String[] getArrayProps() {  
  23.         return arrayProps;  
  24.     }  
  25.     publicvoid setArrayProps(String[] arrayProps) {  
  26.         this.arrayProps = arrayProps;  
  27.     }  
  28.     public Map<String, String> getMapProps() {  
  29.         return mapProps;  
  30.     }  
  31.     publicvoid setMapProps(Map<String, String> mapProps) {  
  32.         this.mapProps = mapProps;  
  33.     }  
  34. }  

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

4、單元測試用例:

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


測試結果:

  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"}