1. Profiles
1.1 brief
spring boot by default will use a global configuration file, the configuration file can be in two formats of files, one is .properties, one is .yaml format files; and the name of the configuration file is fixed, either application. properties, either application.yaml;
Profile role: Spring Boot modify the default values configured, for example, the port number and the like;
1.2YAML grammar Introduction
1.2.1 Introduction
YAML: (YAML Is not Markup Lanuage), the popular name has two meanings:
YAML A Markup Lanuage: YAML is a markup language;
YAML is not Markup Lanuage: YAMA is not a markup language;
YAML data-centric than json.xml more suitable for the configuration file;
For example, we want to change the port information of the start of spring boot, we application.properties file syntax is as follows:
server.port=8971
However application.yaml file syntax is as follows:
server:
port: 8089
Of course, if we have the same work in XML configuration file, the syntax is as follows:
<server> <port>8089</port> </server>
1.2.2 concrete syntax
[1] k :( space) v: designates a pair of key-value pairs (there must be a space), with spaces to indent control hierarchy; as long as one is left-aligned data, are of the same level;
server:
port: 8089
error:
path: /hello
Wherein the first level is a server, port and belong to the same error level, path belonging to a single level;
It is also to be noted that, yaml file attributes and values case-sensitive;
[2] written value
That is all what value can be written in yaml file, our common values types are as follows:
The first category: literal, Common values (numbers, strings, Boolean)
k: v: literal directly to write, especially for string types without adding double quotes, double quotes if there is a special meaning, from strings of special characters special meaning output, such as output "ab \ nc", output:
ab
c
If you add a single quotation mark, then the special characters will be output as, for example: 'ab \ nc' output is:
ab \n c
The second category: objects, map (attributes and values) (key-on);
k: v: to write the next line object relationship attributes and values, attention indent; for example, we now have a class User, which has the properties name and age written as follows:
user:
name: haige
age: 20
Above this wording also equivalent to the line following wording:
User{name: haige,age: 20}
The third category: the collection array (List, Set);
Represents an element in the array values, for example below sets pets, there are a set of original pixel - with: pig, dog, cat worded as follows:
pets:
-pig
-dog
-cat
Writing said further line is equivalent to the wording of this following:
pets: [pig,cat,dog]
1.3 injecting property value from the configuration file YAML
Since YAML configuration file so convenient that we need to learn to read properties from a YAML file. Here, an example presentation;
Specific steps are as follows:
[Step 1]: the Person class declaration, there are attributes name, age, map set, list collection, objects and other attributes;
package com.hai.bao.springboot01; import java.util.Date; import java.util.List; import java.util.Map; /** * @author :haibao.wang * @date :Created in 2019/8/29 22:20 * @description:person类 * @modified By: * @version: $ */ public class Person { private String name; private int age; private boolean boss; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isBoss() { 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{" + "name='" + name + '\'' + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }
Person class which has a class attribute Dog, Dog class as follows:
package com.hai.bao.springboot01; /** * @author :haibao.wang * @date :Created in 2019/8/29 22:23 * @description:新建一个小狗类 * @modified By: * @version: $ */ public class Dog { private String dogName; private int dogAge; public String getDogName() { return dogName; } public void setDogName(String dogName) { this.dogName = dogName; } public int getDogAge() { return dogAge; } public void setDogAge(int dogAge) { this.dogAge = dogAge; } }
[2] configuration attribute value associated application.yaml in the Person class file;
Server:
Port: 8089
Person:
name: Haige
Age: 18 is
BOSS: to true
Birth: 1990/05/27
Maps:
key01: Chemical
key02: Physical
Lists:
- Running
- Swimming
Dog:
dogName: Cai
dogAge: 3
[3] At this time there is a message in the Person class:
At this time, the processor needs to be added in the profile pom.xml file, as dependent;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
[4] Person class to add @ConfigurationProperties (prefix = "Person") notes. Tell Spring boot configuration information for all the properties and configuration files in this class bind one by one;
[5] Person class to add annotations container, ensure that the components in the container, it can be used @ConfigurationProperties annotations;
[6] Start Spring boot services;
[7] to write a test class Spring boot unit to verify the feature:
package com.hai.bao; import com.hai.bao.springboot01.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 BaoApplicationTests { @Autowired Person person; @Test public void contextLoads() { System.out.println(person); } }
Code results are as follows:
1.4 .properties file to configure attributes of the Person class;
# Configure the Person class-related property values person.age = 10 person.birth = 1990 / 05 / 01 person.boss = to true person.maps.key01 = mathematics person.maps.key02 = chemical person.lists = yoga, swimming person .dog.dogName = Cai person.dog.dogAge = 2
After starting the execution unit testing, the test results are as follows:
Garbled, because the method IDEA native coding format utf-8, to solve distortion problems are as follows:
File-settings-File Encodings