One, yaml
Spring boot uses a global configuration file, which is fixed. It can be application.xml or application.yaml, placed in the /resources or /config directory
- yaml: is a markup language, data-centric, more suitable for configuration files than xml and json
- XML configuration example:
<server>
<port> 8081</port>
</server>
- yml configuration example:
server:
port: 8085
path: /hello
2.yaml writing
Reference syntax: https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-yamlsyntax
Literal variables: single, indivisible value (number, string, boolean, date)
Object, Map (key:value key-value pair):
person:
name: tom
age: 18
personMap:
username: herry
age: 20
person:{name: tom,age: 18}
Array: List, Set
ainamals:
- dog
- cat
- snack
animals:[dog,cat,snack]
Three, yaml test
1. Import the configuration file processor, there will be a prompt when writing the configuration
<!-- 导入文件处理器,配置文件进行绑定的时候,就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.java bean
public class Dog {
private String name;
private Integer age;
}
/**
* 将配置文件中的每一个值,映射到这个组件中
*@ConfigurationProperties: 此注解表示告诉sprigboot将本类中的
* 所有属性和配置文件中相关配置进行一一绑定
*prefix = "person" :配置文件中的哪个进行绑定
*@Component: 只有是容器中的组件才会作用
*
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean boss;
private Date birthday;
private Map<String,Object> map;
private List<Object> list;
private Dog dog;
}
3. Configuration file injection
application.yml
person:
name: tom
age: 17
boss: true
birthday: 2018/01/12
map:
k1: v1
k2: v2
list:
- cat
- fish
- dog
dog:
name: goodboy
age: 3
application.properties
#配置properties属性
# idea properties默认是utf-8
person.name=tim
person.age=16
person.boss=false
person.birthday=2014/11/23
person.map.k1=v1
person.map.k2=22
person.list=cat,dog,fish
person.dog.name=goodboy
person.dog.age=3
4. Test the class, start it, and observe whether the console prints
/**
* Spring boot 单元测试
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootHelloworldQuickApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
The default utf-8 properties configuration file in idea may be garbled
- Grammar reference and verification address: http://www.yaml.org/ https://nodeca.github.io/js-yaml/
-
Two, yaml syntax specification
1. Basic grammar of yaml
- Use indentation to indicate hierarchical relationships
- Tab key is not allowed when indenting, only spaces are allowed. Spaces to control the hierarchy
- k: ( space ) v: represents a pair of key-value pairs ( spaces must be present );
- The number of indented spaces is not important, as long as the elements of the same level are aligned to the left
- Case Sensitive
- k: v: write directly literally
- String:
silently do not add single quotation marks or double quotation marks
"": Double quotation marks do not escape special characters in the string, special characters will express their own meaning. For example, "Li" Li Si\n 20 years old" Output: Li Si wraps 20 years old
'': Single quote; special characters will be escaped, and special characters are ultimately just ordinary string data. Such as "Li" Li Si\n 20 Years old" Output: Li Si\n 20 years old- File: Multiple files are separated by ---
- The object is key: value, separated by a colon
- Use a space after the colon to separate the key value
- Inline writing:
- - represents an element in the array
- Arrays and objects and combined use
- Inline writing