第二章 Spring Boot 配置
1、YAML 配置
SpringBoot 全局配置文件
application.properties application.yml • 1 • 2
YAML 以数据为中心,比 json、xml 更适合作为配置文件
server: port: 8081
<server> <port>8081</port> </server>
2、YAML 语法
YAML 语言教程:
http://www.ruanyifeng.com/blog/2016/07/yaml.html
1、基本语法
key:空格value
空格缩进来控制层级关系,左对齐的数据就是一个层级
属性和值大小写敏感
空格必须有
2、值的写法
2.1、字面量:普通的值(数字,字符串,布尔)
字符串默认不用加单引号或者双引号
(1)""双引号不会转义特殊字符。特殊字符会作为本身想表达的意思
eg:
name: "张三\n李四"
输出:
张三[换行]
李四
(2)’'单引号,会转义特殊字符,特殊字符最终只是一个普通的字符串数据
eg:
name: "张三\n李四"
输出:
张三\n李四
2.2、对象,map(属性和值,键值对)
(1)普通写法
person:
name: Tom
age: 23
(2)行内写法
person: { name: Tom, age: 23 }
2.3、数组,(List, Set)
(1)普通写法
pets:
- cat
- dog
- pig
(2)行内写法
pets: [cat, dog, pig]
3、YAML 配置文件中值获取
配置文件
src/main/resources/application.yml
person: lastName: Tom age: 18 boss: false birth: 2017/12/12 maps: { k1: v1, k2: v2 } lists: - cat - dog dog: name: Jack age: 2
映射类
src/main/java/com/mouday/bean/Person.java
package com.mouday.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; /** * 将配置文件中的属性映射到这个组件中 */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; private Boolean sex; private Date birth; private Map<String, Object> maps; private List<String> lists; private Dog dog; 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; } public Boolean getSex() { return sex; } public void setSex(Boolean sex) { this.sex = sex; } 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<String> getLists() { return lists; } public void setLists(List<String> 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 + ", sex=" + sex + ", birth=" + birth + ", maps=" + maps + ", lists=" + lists + ", dog=" + dog + '}'; } }
src/main/java/com/mouday/bean/Dog.java
package com.mouday.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 + '}'; } }
单元测试依赖
pom.xml
<!--配置文件处理器 导入配置文件导入提示--> <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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
单元测试
src/test/java/com/mouday/DemoApplicationTests.java
package com.mouday; import com.mouday.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 DemoApplicationTests { @Autowired private Person person; @Test public void contextLoads() { System.out.println(person); } }
打印结果
Person{name='Tom', age=18, sex=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k1=v1, k2=v2}, lists=[cat, dog], dog=Dog{name='Jack', age=2} }
读取 properties 文件配置
src/main/resources/application.properties
person.name=TOM
person.age=18
person.sex=false
person.birth=2017/12/12
person.maps.k1=v1
person.maps.k2=v2
person.lists=cat,dog
person.dog.name=Jack
person.dog.age=2