In spring boot, a few simple steps to read the various types of property values in the configuration file (APPLICATION.YML):
1, the introduction of dependency:
[HTML]View PlainCopy
- <!--support @ConfigurationProperties annotations--
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactid>spring-boot-configuration-processor</artifactid>
- <optional>true</optional>
- </Dependency>
2. Configure the values for each property in the configuration file (APPLICATION.YML):
[Plain]View PlainCopy
- myprops: #自定义的属性和值
- simpleProp: simplepropvalue
- &NBSP;&NBSP;ARRAYPROPS:&NBSP;1,2,3,4,5&NBSP;&NBSP;
- listprop1:
- - name: abc
- value: abcvalue
- - name: efg
- value: efgvalue
- &NBSP;&NBSP;LISTPROP2:&NBSP;&NBSP;
- - config2value1
- - config2vavlue2
- mapprops:
- key1: value1
- key2: value2
3. Create a bean to receive configuration information:
[Java]View PlainCopy
- @Component
- @ConfigurationProperties (prefix="Myprops") //Receive Myprops properties in Application.yml
- Public class Myprops {
- private String Simpleprop;
- private string[] arrayprops;
- private list<map<string, string>> listProp1 = new arraylist<> (); //Receive the attribute value inside the Prop1
- private List<string> listProp2 = new arraylist<> (); //Receive the attribute value inside the PROP2
- private map<string, string> mapprops = new hashmap<> (); //Receive the attribute value inside the Prop1
- Public String Getsimpleprop () {
- return simpleprop;
- }
- the//string type must need a setter to receive the attribute value; 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 attributes inside the bean will automatically receive the configured values.
4. Unit Test Cases:
[Java]View PlainCopy
- @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:
[Plain]View PlainCopy
- 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
Spring Boot Read property value in configuration file (APPLICATION.YML) 111