Spring Boot Read property value in configuration file (APPLICATION.YML) 111

Source: Internet
Author: User

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
  1. <!--support @ConfigurationProperties annotations--
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactid>spring-boot-configuration-processor</artifactid>
  5. <optional>true</optional>
  6. </Dependency>

2. Configure the values for each property in the configuration file (APPLICATION.YML):

[Plain]View PlainCopy
    1. myprops:  #自定义的属性和值   
    2.   simpleProp:  simplepropvalue  
    3. &NBSP;&NBSP;ARRAYPROPS:&NBSP;1,2,3,4,5&NBSP;&NBSP;
    4.   listprop1:  
    5.     - name: abc   
    6.       value: abcvalue  
    7.      - name: efg  
    8.       value:  efgvalue  
    9. &NBSP;&NBSP;LISTPROP2:&NBSP;&NBSP;
    10.     -  config2value1  
    11.     - config2vavlue2  
    12.   mapprops:  
    13.     key1: value1   
    14.     key2: value2  


3. Create a bean to receive configuration information:

[Java]View PlainCopy
  1. @Component
  2. @ConfigurationProperties (prefix="Myprops") //Receive Myprops properties in Application.yml
  3. Public class Myprops {
  4. private String Simpleprop;
  5. private string[] arrayprops;
  6. private list<map<string, string>> listProp1 = new arraylist<> (); //Receive the attribute value inside the Prop1
  7. private List<string> listProp2 = new arraylist<> (); //Receive the attribute value inside the PROP2
  8. private map<string, string> mapprops = new hashmap<> (); //Receive the attribute value inside the Prop1
  9. Public String Getsimpleprop () {
  10. return simpleprop;
  11. }
  12. the//string type must need a setter to receive the attribute value; maps, collections, and arrays don't need
  13. public void 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. public void Setarrayprops (string[] arrayprops) {
  26. this.arrayprops = arrayprops;
  27. }
  28. Public map<string, string> getmapprops () {
  29. return mapprops;
  30. }
  31. public void Setmapprops (map<string, string> mapprops) {
  32. this.mapprops = mapprops;
  33. }
  34. }

Once started, the attributes inside the bean will automatically receive the configured values.

4. Unit Test Cases:

[Java]View PlainCopy
  1. @Autowired
  2. private Myprops Myprops;
  3. @Test
  4. public void 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. }



Test results:

[Plain]View PlainCopy
    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"}

Source Code Reference: Https://github.com/xujijun/my-spring-boot

Spring Boot Read property value in configuration file (APPLICATION.YML) 111

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.