springboot学习(五)---读取配置文件(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"}  

猜你喜欢

转载自blog.csdn.net/u010775025/article/details/80266847