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)