SpringBoot支持使用一个全局的配置配置文件,目录在src/main/resources或者类路径/config
,全局配置文件可以对一些默认配置值进行修改,配置文件名是固定的:application.properties或application.yml
yaml是YAML语言的文件,以数据为中心,比json、xml等更适合做配置文件 参考官方语法规范
1、YAML语法
server:
port: 8082
path: /hello
2、值的写法
friends:
lastName: aa
age: 20
// 行内写法
friends: {lastName:aa,age:20}
// 用- 表示数组中的一个元素
pets:
- cat
- dog
- pig
// 行内写法
pets: [cat,dog,pig]
public class Dog {
private String name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
}
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(prefix = "person") 告诉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 void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Integer getAge() {
return age;
}
public Date getBirth() {
return birth;
}
public String getLastName() {
return lastName;
}
public Boolean getBoss() {
return boss;
}
public Dog getDog() {
return dog;
}
public List<Object> getLists() {
return lists;
}
public Map<String, Object> getMaps() {
return maps;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
server:
port: 8082
person:
last-name: hello
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
dog:
name: 狗狗
age: 12
import com.sangyu.springboot.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;
/**
* Spring 单元测试
* 可以在测试期间很方便类似编码一样进行自动注入容器等
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringBoot01ApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
可能会提示一个异常
如何解决:在pom.xml中注入
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>