Springboot Getting started three-read configuration information two (read properties file mode)

Source: Internet
Author: User

In the previous article, we briefly introduced the self-contained read method. Springboot provides several ways to read

First, @ConfigurationProperties (value= "my")

Supports more flexible binding and metadata support, but does not support Spel

Define AppConfig receive (requires set Get method)

@Component
@ConfigurationProperties (value = "my")
public class AppConfig {
private String name;
Private String version;
Private String FirstName;
Private String ServerPort;
private int size;
private list<string> roles;
private list<string> servers;

......

}

Application.properties attribute notation (list type has two types):

My.name=dev
my.version=2.0.0
My.firstname=dev-first
my.server-port=9090
My.size=20
My.roles[0]=user
My.roles[1]=admin
My.servers=test.baidu.com,dev.baidu.com

Second, @Value need to be combined with @propertysource annotation, can support spel, there are strict restrictions, not flexible enough, do not support arrays. But can be customized to read external files

Value is an array of paths, followed by a file instead of the previous file, related to the placement position.

@Component
@PropertySource (value={"Classpath:/custom/custom.properties", "File:${apphome}/app-config.properties"},encoding = "UTF-8")
public class Valueconfig {
@Value ("${my.name}")
public String name;
@Value ("${my.version}")
public String version;
@Value ("${my.firstname}")
Public String FirstName;
@Value ("${my.server-port}")
Public String ServerPort;
@Value ("${my.size}")
public int size;
@Value ("#{' ${my.servers} '. Split (', ')}")
public list<string> servers;
}

Springboot Getting started three-read configuration information two (read properties file mode)

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.