加载将配置文件属性映射到java对象中。
1.配置文件
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springboot</groupId>
<artifactId>springboot_02propertys</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.编写application.yml 配置文件。路径在项目根目录下。
server:
port: 8081
person:
name: yy #String 对
maps: {k1: 1,k2: 2} # Map对象
list1: ['1','2'] # list 行内写法
list2:
- 1
- 2
user: # 自定义对象
name: yyy
2.编写Person类和User类
package org.spring;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Component:定义为容器组件
* @PropertySource:加载指定配置文件
* @ConfigurationProperties:加载配置文件中person段内容与Person对象进行绑定
* ps:属性必须有set方法。
* List<Object> lists; 必须制定集合里对象的类型。不然注入失败
*
*/
@Component
@PropertySource(value={"classpath:application.yaml"})
@ConfigurationProperties(prefix="person")
public class Person {
String name;
List<Object> list1;
List<Object> list2;
Map maps;
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
@Override
public String toString() {
return "Person [name=" + name + ", list1=" + list1 + ", list2=" + list2 + ", maps=" + maps + ", user=" + user
+ "]";
}
public List<Object> getList1() {
return list1;
}
public void setList1(List<Object> list1) {
this.list1 = list1;
}
public List<Object> getList2() {
return list2;
}
public void setList2(List<Object> list2) {
this.list2 = list2;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
User user;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.spring;
public class User {
String name;
@Override
public String toString() {
return "User [name=" + name + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.编写自动类
package org.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainSpringBoot {
public static void main(String[] args) {
SpringApplication.run(MainSpringBoot.class,args);
}
}
4.编写测试类
package org.spring;
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 MainTest {
@Autowired
Person person;
@Test
public void funTest(){
System.out.println(person);
}
}
/**
测试必须有启动类
测试类名字不能是Test
funTest测试方法的封装等级必须是共有的
**/
5.控制台结果
Person [name=yy, list1=[1, 2], list2=[1, 2], maps={k1=1, k2=2}, user=User [name=yyy]]