Configuration file

1. Configuration files

SpringBoot uses a global configuration file with a fixed file name;

• application. The properties

• application. Yml

The configuration file is used to: modify the default SpringBoot automatic configuration. SpringBoot is automatically configured for us at the bottom;

YAML (YAML Ain’t Markup Language)

YAML isn't A Markup Language. YAML isn't A Markup Language.Copy the code

Markup language:

Previous configuration files; Most use the xxxx.xml file; YAML: Data-centric, more suitable for configuration files than JSON, XML, etc. YAML: Configuration example Server: port: 8081 XML: <server> <port>8081</port> </server>Copy the code





The two configurations have the same effect

2. YAML syntax:

1. Basic grammar

K :(space)v: indicates a pair of key-value pairs (Spaces must exist).

Control hierarchy with space indented; All left-aligned columns are of the same level

server:
    port: 8081
    path: /hello
Copy the code

Properties and values are also case-sensitive; 2, value writing

Literals: ordinary values (numbers, strings, booleans)

K: V: write it literally; Strings do not use single or double quotation marks by default. "" : double quotation marks; Does not escape special characters inside the string; Name: "zhangsan \n lisi" : output; Zhangsan linefeed lisi ": single quotation marks; The special characters are escaped, and the special characters are just a common string data name: 'zhangsan \n lisi' : output; zhangsan \n lisiCopy the code

Objects, maps (attributes and values) (key-value pairs) :

K: v: Writes the relationship between the object's properties and values on the next line; Note that the indent object is still k: v friends: lastName: zhangsan age: 20Copy the code

Written in line:

friends: {lastName: zhangsan,age: 18}
Copy the code

Array (List, Set) :

Represents an element in an array with a – value

pets:
 - cat
 - dog
 - pig
Copy the code

Inline writing

pets: [cat,dog,pig]
Copy the code

Note:

Idea provides shortcut keys for get and set: Alt+Insert. You can also right-click to call out





The format above corresponds to the value below

3. Config file value injection

The configuration file

person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1: v1,k2: 12} lists: - lisi - zhaoliu dog: name: The dog age: 12Copy the code

Javabeans:

@configurationProperties: tells SpringBoot to bind all properties in this class to the configuration file. * prefix = "person" : which of the following attributes in the configuration file are mapped one by one * * The @ConfigurationProperties feature provided by the container can only be configured if the component is a component in the container; * */ @Component @ConfigurationProperties(prefix = "person") 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;Copy the code

We can import the configuration file handler and be prompted to write the configuration later

<! Import configuration file handler, > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>Copy the code

Case study:



Person.java

package com.xdr.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; ConfigurationProperties: Tells SpringBoot to bind all properties in the class to the relevant configurations in the configuration file Prefix ="person": which of the following attributes in the configuration file are mapped one by one / @component @ConfigurationProperties (prefix = "person") 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; public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getBoss() { return boss; } public void setBoss(Boolean boss) { this.boss = boss; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "Person{" + "lastName='" + lastName + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; }}Copy the code

Dog.java

package com.xdr.bean; public class Dog { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Dog{" + "name='" + name + '\'' + ", age=" + age + '}'; }}Copy the code



Add dependencies in pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>
Copy the code

pom.xml

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.1.7. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.xdr</groupId> <artifactId>02_springboot_config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>02_springboot_config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <! Import configuration file handler, > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

Rerun the main program to see if it still has the error

Open the unit tests to see if the values configured above print out applicationTests.java

package com.xdr; import com.xdr.bean.Person; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @runWith (springrunner.class) @Springboottest Public class ApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); }}Copy the code