How does springboot read attribute values in the configuration file (ES1en.yml)
- 2020-06-23 00:23:35
- OfStack
In spring boot, take a few simple steps to read the various types of attribute values in the configuration file (ES3en.yml) :
1. Introduction of dependencies:
<!-- support @ConfigurationProperties annotations -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2. Configure the values of each attribute in the configuration file (application.yml) :
myProps: # Custom properties and values
simpleProp: simplePropValue
arrayProps: 1,2,3,4,5
listProp1:
- name: abc
value: abcValue
- name: efg
value: efgValue
listProp2:
- config2Value1
- config2Vavlue2
mapProps:
key1: value1
key2: value2
3. Create an bean to receive configuration information:
@Component
@ConfigurationProperties(prefix="myProps") // receive application.yml In the myProps The following properties
public class MyProps {
private String simpleProp;
private String[] arrayProps;
private List<Map<String, String>> listProp1 = new ArrayList<>(); // receive prop1 The value of the property inside
private List<String> listProp2 = new ArrayList<>(); // receive prop2 The value of the property inside
private Map<String, String> mapProps = new HashMap<>(); // receive prop1 The value of the property inside
public String getSimpleProp() {
return simpleProp;
}
//String The type of 1 Will need to setter To receive the value of the property; maps, collections, and arrays Don't need
public void setSimpleProp(String simpleProp) {
this.simpleProp = simpleProp;
}
public List<Map<String, String>> getListProp1() {
return listProp1;
}
public List<String> getListProp2() {
return listProp2;
}
public String[] getArrayProps() {
return arrayProps;
}
public void setArrayProps(String[] arrayProps) {
this.arrayProps = arrayProps;
}
public Map<String, String> getMapProps() {
return mapProps;
}
public void setMapProps(Map<String, String> mapProps) {
this.mapProps = mapProps;
}
}
Once started, the properties in bean will automatically receive the configured values.
4. Unit Test Cases:
@Autowired
private MyProps myProps;
@Test
public void propsTest() throws JsonProcessingException {
System.out.println("simpleProp: " + myProps.getSimpleProp());
System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
}
Test results:
simpleProp: simplePropValue
arrayProps: ["1","2","3","4","5"]
listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
listProp2: ["config2Value1","config2Vavlue2"]
mapProps: {"key1":"value1","key2":"value2"}
Source code reference: https: / / github com xujijun/my spring -- boot