1、編寫SpringBoot的引導類
package springboot_test.springboot_test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
2、編寫普通實體Dog
package springboot_test.springboot_test.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//普通實體,會做為Person類中的屬性使用,不需要加任何注解
//@ConfigurationProperties(prefix = "person.dog")
//@Component
public class Dog {
private String name;
private String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
'}';
}
}
3、編寫需要綁定yml文件中的值的配置類Person
package springboot_test.springboot_test.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
//prefix指定使用yml配置文件中的哪部分開頭的數據
@ConfigurationProperties(prefix = "person")
//該類也做為組件才能使用容器中的其他組件,即才能使用@ConfigurationProperties
@Component
public class Person {
private String name;
private String age;
private Date birthDay;
private Map map;
private List list;
//類中含有其他類的對象,想讓dog在yml編寫的時候也給提示,需要加上該屬性
@NestedConfigurationProperty
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", birthDay=" + birthDay +
", map=" + map +
", list=" + list +
", dog=" + dog +
'}';
}
}
4、配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--依賴的父項目,父項目中還會依賴其他父項目來決定使用的jar包的版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>springboot_test</groupId>
<artifactId>springboot_test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--用於引進springmvc等相關jar包,可通過頁面訪問-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用於junit測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--用於熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--配置處理器,當類上加@ConfigurationProperties注解時,需要配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!--用於使用maven的package功能進行打包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5、編寫application.yml文件
person:
name: zhangsan
age: 18
birth-day: 2019/11/19
map: {k1: v1,k2: v2}
list:
- pig
- cat
dog:
color: black
name: 小狗
6、編寫PersonTest.java測試類
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;
import springboot_test.springboot_test.SpringbootTestApplication;
import springboot_test.springboot_test.bean.Person;
@SpringBootTest(classes = SpringbootTestApplication.class)
@RunWith(SpringRunner.class)
public class PersonTest {
@Autowired
private Person person;
@Test
public void getPerson(){
System.out.println(person);
}
}
另外在測試的時候出現了報錯java.lang.Exception: No runnable methods,原因為@Test注解引入的類不對,應該引入org.junit.Test;
還有一個可能的原因是沒有在方法上寫@Test注解
另外,如果綁定的值是在application.properties中配置的,而不是在yml中配置的,則會出現打印結果中文亂碼的問題,application.properties配置如下:
person.name=張三
person.age=18
person.birth-day= 2019/11/19
person.list=a,b,c
person.map.k1=v1
person.map.k2=v2
person.dog.name=小狗
person.dog.color=black
可通過如下所示

另外為了通過配置文件給某個類綁定某個屬性值,不應該通過application.properties去綁定,而應該單獨配置一個properties,
但是單獨配置的properties,SpringBoot是無法識別的,需要在綁定的類Person.java上加上如下注解:
@PropertySource(value = "classpath:person.properties"),即
1、resources下新建person.properties文件內容如下:
person.name=張三
person.age=18
person.birth-day= 2019/11/19
person.list=a,b,c
person.map.k1=v1
person.map.k2=v2
person.dog.name=小黃
person.dog.color=black
2、Person.java改為如下:
package springboot_test.springboot_test.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
//prefix指定使用yml配置文件中的哪部分開頭的數據
@ConfigurationProperties(prefix = "person")
//該類也做為組件才能使用容器中的其他組件,即才能使用@ConfigurationProperties
@Component
//作用是使其可以與person.properties文件中的內容,但是,若application.properties中也存在可綁定的內容,
// 則以application.properties中的內容為准,若不存在,才會以person.properties中的內容為准
@PropertySource(value = "classpath:person.properties")
public class Person {
private String name;
private String age;
private Date birthDay;
private Map map;
private List list;
//類中含有其他類的對象,想讓dog在yml編寫的時候也給提示,需要加上該屬性
@NestedConfigurationProperty
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", birthDay=" + birthDay +
", map=" + map +
", list=" + list +
", dog=" + dog +
'}';
}
}
如有理解不到位的地方,望指教!