Springboot reading a custom profile node

Source: Internet
Author: User

Today and everyone to share is the custom configuration information read, recently wrote a blog such plans, respectively, cross to write springboot aspects and Springcloud aspects of the article, because Springboot is expected to be a lot of chapters, so the cloud will need to wait for the article to write The main reason for sharing the two articles is to make it easier for them to find the information and to make it convenient for the friends who will use it.

    • @Value Tag Read (the APPLICATION.YML node can be read directly by default)
    • node for entity mapping application.yml
    • Entity mapping node for custom configuration files
    • Entity mapping values for multi-level nodes
@Value Tag Read (the APPLICATION.YML node can be read directly by default)

The following node information is first customized in the default application.yml configuration file:

1 Shenniu: 2   Name: God OX 003

The attribute values are then directly obtained through the @value tag, and a/conf/name interface is defined for better presentation of the data to the interface:

 1  @Value ( ${shenniu.name}   )  2  private   String name;  3   " /conf/defname  "  )  5  public   String Getdefname () { 6  return  Span style= "color: #0000ff;" >this  .name;  7 } 

Post-run effects such as:

  

node for entity mapping application.yml

To map the default profile value with the entity class, just add a property name prefix, which is only for the properties in the application.yml default file:

1 @Configuration2@ConfigurationProperties (prefix ="Shenniu")3  Public classshenniuconf {4 5     PrivateString name;6 7      PublicString GetName () {8         returnname;9     }Ten  One      Public voidsetName (String name) { A          This. Name =name; -     } -  the     PrivateURL url; -  -      PublicUrl GetUrl () { -         returnURL; +     } -  +      Public voidseturl (url url) { A          This. url =URL; at     } -  -     PrivateString des; -  -      PublicString getdes () { -         returndes; in     } -  to      Public voidsetdes (String des) { +          This. des =des; -     } the  *      Public Static classURL { $ Panax Notoginseng         PrivateString Blogurl; -  the          PublicString Getblogurl () { +             returnBlogurl; A         } the  +          Public voidSetblogurl (String blogurl) { -              This. Blogurl =Blogurl; $         } $  -     } -}

Entity Class I added a few more properties, for later testing convenience; To do configuration mapping to application.yml only need to add   @ConfigurationProperties (prefix = Span style= "color: #800000;" > " shenniu " )   tags, The value of the prefix corresponds to the prefix of the custom profile property, where the/conf/name interface output is added to map out the value of the object:

1 @Autowired 2     Private shenniuconf shenniuconf; 3 4     @GetMapping ("/conf/name")5public       String GetName () {6         return  shenniuconf.getname (); 7     }

The same effect as the @values annotation, which indicates that the entity and profile custom attributes match successfully, and that the primary entity attribute name should be consistent with the profile node name;

  

Entity mapping node for custom configuration files

It is very simple to read the property values in Application.yml, and sometimes you need to customize your own configuration files, such as my my.properties here, the file contents are as follows:

1 shenniu.des= I love my motherland 2 shenniu.url.blogurl=http://www.a.com

The mapped entity is still using the above, but the entity needs to add annotations @PropertySource ("classpath:my.properties") to represent the custom file source, and also need prefix to specify the node prefix; we add the following display interface:

1 @Autowired 2     Private shenniuconf shenniuconf; 3 4     @GetMapping ("/conf/des")5public       String getdes () {6         return  shenniuconf.getdes (); 7     }

  

Entity mapping values for multi-level nodes

Usually the custom configuration node has a hierarchical relationship, so how can our entities get to different levels of attribute values, in fact, only need to be in the entity class through the class level relationship to represent the line, such as the above shenniuconf entity inside the URL entity class attributes;

  

Here you add a test interface:

1     @Autowired 2     Private shenniuconf shenniuconf; 3     4     @GetMapping ("/conf/myconf")5      Public shenniuconf Getblogurl () {6         return shenniuconf; 7     }

Not surprisingly, the following results can be obtained:

  

Springboot reading a custom profile node

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.