Configuration File
- There are two kind of file format can be used in Spring Boot
- application.properties
- application.yml (recommended)
Why Need Config File
- To modify defaule value which be initialized by Spring Boot.
YAML vs. XML vs. properties
-
There are three kind of configuration file type we usually.
-
YAML
-
server: prot: 8081
-
-
XML
-
<server> <prot>8081</prot> </server>
-
-
properties
-
server.prot: 8081
-
YAML Expression Syntax
-
Presents Number, String, Boolean value
-
double quotes " "
input: name: "daniel \n cool" \n ζ’θ‘
-
output: daniel ζ’θ‘ cool
-
single quotes ' '
input: name: "daniel \n cool"
- output: daniel \n cool
-
-
Presents Objece, Map
-
friends: lastName: daniel age: 21
-
friends: {lastName: daniel,age: 21}
-
-
Presents List, Set
-
pets: - cat - dog - pig
-
pets: [cat,dog,pig]
-
-
Sample
-
person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: β lisi β zhaoliu dog: name: ε°η age: 12
-
Build Relationship Between YAML and Class
-
Sample
-
@Component // make this to container component, so that to use @ConfigurationProperties @ConfigurationProperties(prefix="person") //make binding between YAML and this class, prefix is used to specify a target attribute public class Person { private String lastName; private Integer age; private Boolean boss; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog; // this is a object }
-