3.1 ConfigurationProperties 应用形式
3.1.1. 步骤
@Component+@ConfigurationProperties(prefix="person")
person 是在 yml 中配置的前缀: person: …
3.1.2. 样例
(1). maven 依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.niewj</groupId>
<artifactId>springboot-study</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<spring-boot-version>2.3.2.RELEASE</spring-boot-version>
</properties>
<modules>
<module>springboot-01-helloworld</module>
</modules>
<!-- 示意是一个 pom 总的父工程 -->
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<!-- springboot 测试用例须要的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<!-- ConfigurationProperties 属性配置工具依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<!-- json 依赖 -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 解决 ConfigurationProperties 报错 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2). 样例之:person.java
package com.niewj.springboot.model;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Created by niewj on 2020/8/5 0:01
*/
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean male;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
}
(3). Dog.java
package com.niewj.springboot.model;
import lombok.Data;
/**
* Created by niewj on 2020/8/5 0:04
*/
@Data
public class Dog {
private String name;
private Integer age;
}
(4). application.yml
server:
port: 8888
person:
lastName: 张三
age: 33
male: true
birth: 1985/03/03
maps: {k1: v1, k2: 20, k3: true}
lists:
- lisi
- wangwu
dog:
name: 小黑
age: 3
(5). 测试用例:
package com.niewj.springboot;
import com.google.gson.Gson;
import com.niewj.springboot.model.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;
/**
* Created by niewj on 2020/8/5 0:19
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestProperties {
@Autowired
private Person person;
@Test
public void printPerson(){System.out.println(new Gson().toJson(person));
}
}
(6). 输入:
{
"lastName": "张三",
"age": 33,
"male": true,
"birth": "Mar 3, 1985 12:00:00 AM",
"maps": {
"k1": "v1",
"k2": 20,
"k3": true
},
"lists": ["lisi", "wangwu"],
"dog": {
"name": "小黑",
"age": 3
}
}
3.2 @ConfigurationProperties 和 @Value 比拟
3.2.1 @ConfiguratonProperties
- 适宜场景: 批量注入配置文件中的属性;
-
反对 Relax-Binding(送伞绑定);
如
Person.firstName
匹配:person.first-name
person.first_name
person.firstName
PERSON_FIRST_NAME
- 不反对 spEL 表达式;
- JSR303 数据校验反对;
- 反对简单类型: 能够取到 maps 的值;
3.2.2 @Value
- 适宜场景: 一个一个指定配置属性;
- 不反对 Relax-Binding (涣散绑定);
- 反对 spEL;
如 @Value(“#{20*2}”) // 会计算出 40 赋值给字段属性
- 不反对 JSR303 数据校验;
- @Value 取不到 maps 的值 (不反对简单类型)