Springboot external Configuration < medium > Yaml file instead of property file

Source: Internet
Author: User

Yaml is a JSON-formatted extension set and is a very handy file format for defining properties. When your project has a Snakeyaml library, Spring boot will automatically support it.

1. Using YAML files

There are two classes in springframework that can load a Yaml file, which YamlPropertiesFactoryBean can be loaded as a property class Properties and YamlMapFactoryBean can be loaded as a map.

Loading YAML files

The following Yaml file

environments:    Dev:        url:http://dev.bar.com        name:developer Setup    Prod:        url:http://foo.bar.com        name:my Cool App

Convert to property file as

Environments.dev.url=http://dev.bar.comenvironments.dev.name=Developer Setupenvironments.prod.url=http://foo.bar.comenvironments.prod.name=my Cool App

The Yaml list will be converted to a key with the [index] string, as

my:   servers:       - dev.bar.com       -foo.bar.com

will be converted to

my.servers[0]=dev.bar.commy.servers[1]=foo.bar.com

If you want to bind an object to a list or set property in an entity bean like the Spring DataBinder tool (which uses the @configurationproperties annotation here), you do not need to provide a setter or initialize the property for the property to be changed. You just need to do the following.

@ConfigurationProperties (prefix= "my")publicclass  Config {    private  New arraylist<string>();      Public List<string> getservers () {        returnthis. servers;    }}

2. Exposing YAML files to spring environment

The Yaml file is typically exposed to spring environment as a property resource file with Yamlpropertysourceloader. This enables you to access these environment variables by using the @value comment + placeholder syntax.

3. Multiple configuration of YAML documents.

You can configure multiple duplicate YAML documents in a single folder and use spring.profiles values to indicate which one you want to use, for example:

Server:     192.168.1.100---spring:    profiles:developmentserver:    127.0.0.1---spring:    Profiles: ProductionServer:    192.168.1.120

In the example above, the server address in the development environment would be 127.0.0.1, and the server address for the production environment would be 192.168.1.100 If spring's profiles is not available, then 192.168.1.100.

Profiles is usually active without the specified condition. So in this file we set up a security.user.password that is only valid for the "default" profiles.

Server:   8000---spring:  defaultsecurity:  User:    password:weak

In the example below, the password is always set because it is not bound to any profiles, so it will be reset in all other profiles.

4. Yaml drawbacks

Yaml files cannot be loaded by @propertysource annotations.

5. Combination of YAML list

From the above we can see that YAML is eventually converted into properties. When a list property is overwritten by profiles, the program becomes a counter to add data to the list.

For example, suppose the class Mypojo contains two default null properties name and discretion, let's expose them through the Fooproperties class.

@ConfigurationProperties ("foo")publicclass  fooproperties {    private  finalnew arraylist<>();      Public List<mypojo> getList () {        returnthis. List;    }}

Consider the following configuration

foo:  list:    - name:my name      description:my description---spring:  Profiles:devfoo:  list:    -Name:my another name

If Dev is active, Fooproperties.list contains only one entry that is defined above. If Dev is active, it contains only the entry defined below. It will not merge.

Springboot external Configuration < medium > Yaml file instead of property file

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.