1、YAML(YAML Ain't Markup Language)
4.1.1 通过 @ConfigurationProperties 注解
4.2.1 通过 @ConfigurationProperties 注解
结论:
1、如果业务逻辑中,只从配置文件中读取某项值,使用@Value
2、如果专门编写了JavaBean来和配置文件映射,使用@ConfigurationProperties
一、配置文件
1、配置文件的种类
SpringBoot使用一个全局的配置文件,配置文件名是固定的;
application.properties
server.port=8081
application.yml
server:
port: 8081
2、配置文件的作用
修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;
二、YAML简介
1、YAML(YAML Ain't Markup Language)
- YAML A Markup Language:是一个标记语言
- YAML isn't Markup Language:不是一个标记语言;
- YAML:以数据为中心,比json、xml等更适合做配置文件;
举个例子:
YAML
server:
port: 8081
XML:
<server>
<port>8081</port>
</server>
三、YAML语法
1、语法格式
k:空格v
表示一对键值对(空格必须有);
- 使用缩进表示层级关系
- 缩进时不允许使用Tab键,只允许使用空格
- 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
- 大小写敏感
2、值的类型
2.1 普通值(数字|字符串|布尔值)
k: v:字面直接来写;
注意:
- 字符串默认不用加上单引号或者双引号;
- "":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
- name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
- '':单引号;会转义特殊字符,特殊字符终只是一个普通的字符串数据
- name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
- "":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
2.2 对象|map
k: v:在下一行来写对象的属性和值的关系;注意缩进
- 缩进写法
friends:
lastName: zhangsan
age: 20
- 行内写法
friends: {lastName: zhangsan,age: 18}
2.3数组|集合
- 缩进写法:
lists:
- lisi
- zhaoliu
- 行内写法:
lists: [lisi,zhaoliu]
四、获取配置文件中的值
4.1 获取yml文件中的值
application.yml配置文件
person:
lastName: zhangsan
age: 18
boss: false
birth: 2019/4/29
maps: {k1: v1,k2: l2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 2
4.1.1 通过 @ConfigurationProperties 注解
POJO类
package com.itheima.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;
/**
* @author jsz
* @create 2019/4/29
* 将配置文件总配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties :
* 使用前提:只有这个组件是容器中的组件,才能使用@ConfigurationProperties的功能
* 告诉springBoot将本类中所有的属性和配置文件中相关的配置进行绑定
* prefix = "person": 配置文件中哪个下面的所有属性进行一一映射
*
*/
@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 +
'}';
}
}
测试类
package com.itheima;
import com.itheima.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;
/**
* springBoot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
private Person person;
@Test
public void testHelloWorld(){
System.out.println(person);
}
@Test
public void contextLoads() {
}
}
4.1.2 通过@Value注解
package com.itheima.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author jsz
* @create 2019/4/29
* <bean class="Person">
* <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/# {SpEL}"></property>
* <bean/>
*/
@Component
public class Person {
@Value("${person.lastName}")
private String lastName;
@Value("#{2*9}")
private Integer age;
@Value("true")
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 +
'}';
}
}
测试类
package com.itheima;
import com.itheima.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;
/**
* springBoot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
private Person person;
@Test
public void testHelloWorld(){
System.out.println(person);
}
@Test
public void contextLoads() {
}
}
4.2 获取properties文件中的值
application.properties配置文件
#配置person的值
person.last-name=张三
person.age=18
person.birth=2019/4/30
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=小狗
person.dog.age=15
需要修改properties文件的编码
4.2.1 通过 @ConfigurationProperties 注解
POJO类
package com.itheima.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;
/**
* @author jsz
* @create 2019/4/29
* 将配置文件总配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties :
* 使用前提:只有这个组件是容器中的组件,才能使用@ConfigurationProperties的功能
* 告诉springBoot将本类中所有的属性和配置文件中相关的配置进行绑定
* prefix = "person": 配置文件中哪个下面的所有属性进行一一映射
*
*/
@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 +
'}';
}
}
测试类
package com.itheima;
import com.itheima.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;
/**
* springBoot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
private Person person;
@Test
public void testHelloWorld(){
System.out.println(person);
}
@Test
public void contextLoads() {
}
}
4.2.2 通过 @Value 注解
POJO类
package com.itheima.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author jsz
* @create 2019/4/29
* <bean class="Person">
* <property name="lastName" value="字面量/${key}从环境变量、配置文件中获取值/# {SpEL}"></property>
* <bean/>
*/
@Component
public class Person {
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
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 +
'}';
}
}
测试类
package com.itheima;
import com.itheima.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;
/**
* springBoot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
private Person person;
@Test
public void testHelloWorld(){
System.out.println(person);
}
@Test
public void contextLoads() {
}
}
区别
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个制定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
4.3 配置文件注入值---数据校验功能
YML文件
person:
lastName: zhangsan
age: 18
boss: false
birth: 2019/4/29
maps: {k1: v1,k2: l2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 2
package com.itheima.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Email;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author jsz
* @create 2019/4/29
* 将配置文件总配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties :
* 使用前提:只有这个组件是容器中的组件,才能使用@ConfigurationProperties的功能
* 告诉springBoot将本类中所有的属性和配置文件中相关的配置进行绑定
* prefix = "person": 配置文件中哪个下面的所有属性进行一一映射
*
*/
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
@Email//lastName必须是邮箱格式
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 +
'}';
}
}
package com.itheima;
import com.itheima.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;
/**
* springBoot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot01ApplicationTests {
@Autowired
private Person person;
@Test
public void testHelloWorld(){
System.out.println(person);
}
@Test
public void contextLoads() {
}
}