SpringBoot 多种读取配置文件中参数的方式(小豆丁技术栈)

目录
  1. 1. 一、简介
    1. 1.1. 1、SpringBoot 中常用读取配置方法
    2. 1.2. 2、@Value 和 @ConfigurationProperties 的区别
  2. 2. 二、使用 @Value 读取配置
    1. 2.1. 1、@Value 读取配置参数
    2. 2.2. 2、@Value 给参数设定值
    3. 2.3. 3、@Value 读取系统属性
    4. 2.4. 4、@Value 读取 Bean 的属性
    5. 2.5. 5、@Value 使用 SpEL 表达式
    6. 2.6. 6、@Value 读取 Resource 资源文件
  3. 3. 三、使用 @ConfigurationProperties 读取配置
    1. 3.1. 1、@ConfigurationProperties 读取配置参数到 String 类型
    2. 3.2. 2、@ConfigurationProperties 读取 List 类型参数
    3. 3.3. 3、@ConfigurationProperties 读取 Map 类型参数
    4. 3.4. 4、@ConfigurationProperties 读取 Time 类型参数
    5. 3.5. 5、@ConfigurationProperties 读取 DataSize 类型参数
    6. 3.6. 6、@ConfigurationProperties 读取 DataSize 类型参数
    7. 3.7. 7、@ConfigurationProperties 读取配置参数并进行 Valid 效验
    8. 3.8. 8、@ConfigurationProperties 读取配置到新建 Spring Bean 中
    9. 3.9. 9、从指定配置文件中读取参数
  4. 4. 四、使用 Environment 对象读取配置
  5. 5. 五、使用 PropertiesLoaderUtils 读取配置
  6. 6. 六、读取配置文件示例项目
    1. 6.1. 1、Maven 引入相关依赖
    2. 6.2. 2、测试的配置文件
    3. 6.3. 3、读取配置的多种方法
      1. 6.3.1. (1)、读取 String 配置的 Service
      2. 6.3.2. (2)、读取 List 配置的 Service
      3. 6.3.3. (3)、读取 Map 配置的 Service
      4. 6.3.4. (4)、读取 Time 的 Service
      5. 6.3.5. (5)、读取 DataSize 的 Service
      6. 6.3.6. (6)、读取配置并进行 valie 效验的 service
      7. 6.3.7. (7)、读取配置到 Spring Bean 对象中
    4. 6.4. 4、配置测试用的 Controller
      1. 6.4.1. (1)、读取 String 的 Controller
      2. 6.4.2. (2)、读取 List 的 Controller
      3. 6.4.3. (3)、读取 Map 的 Controller
      4. 6.4.4. (4)、读取 Time 的 Controller
      5. 6.4.5. (5)、读取 Datasize 的 Controller
      6. 6.4.6. (6)、读取 Datasize 的 Controller
      7. 6.4.7. (7)、读取参数到 Spring Bean 对象的 Controller
    5. 6.5. 5、启动类

一、简介

在日常开发使用 SpringBoot 框架时,经常有一些配置信息需要放置到配置文件中,我们需要手动读取这些配置到应用中进行一些逻辑,这里整理了一些常用读取配置的方法,简单介绍一下。

1、SpringBoot 中常用读取配置方法

SpringBoot 中常用的读取配置方法有:

  • 使用 @Value 注解读取配置
  • 使用 @ConfigurationProperties 注解读取配置
  • 使用 Environment 对象读取配置
  • 使用 PropertiesLoaderUtils 工具读取配置

2、@Value 和 @ConfigurationProperties 的区别

二者区别 @ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持

下面会详细介绍使用各个方法是如何读取配置信息。

二、使用 @Value 读取配置

1、@Value 读取配置参数

application.properties 配置文件内容:

1
2
my.name=mydlq
my.age=18

使用 @Value 读取配置文件

1
2
3
4
5
6
7
8
9
10
@Component
public class ReadProperties {

@Value("${my.name}")
private String name;

@Value("${my.age}")
private Integer age;

}

并且还可以设置一个默认值,放置未从配置文件中读取到该参数:

1
2
3
4
5
6
7
8
9
10
11
通过 @Value 读取配置文件
@Component
public class ReadProperties {

@Value("${my.name:默认姓名}")
private String name;

@Value("${my.age:18}")
private Integer age;

}

2、@Value 给参数设定值

使用 @Value 注解给参数设定值,达到跟“=”号一样的赋值效果:

1
2
3
4
5
6
7
@Component
public class ReadProperties {

@Value("#{'test value'}")
private String value;

}

3、@Value 读取系统属性

使用 @Value 注解读取系统环境参数:

1
2
3
4
5
6
7
@Component
public class ReadProperties {

@Value("#{systemProperties['os.name']}")
private String systemPropertiesName;

}

4、@Value 读取 Bean 的属性

测试用的实体对象:

1
2
3
4
5
@Data
public class User{
private String name;
private String age;
}

使用 @Value 注解读取 Bean 中对象的属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class ReadProperties {

@Bean
public User user(){
User user = new User();
user.setName("测试");
user.setAge("18");
return user;
}

@Value("#{user.name}")
private String value;

}

5、@Value 使用 SpEL 表达式

在 @Value 注解中可以使用 SpEL 表达式,如下是使用 SpEL 表达式生成随机数:

1
2
3
4
5
6
7
@Component
public class ReadProperties {

@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double random;

}

6、@Value 读取 Resource 资源文件

使用 @Value 可以读取资源文件进行一些操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Component
public class ReadProperties {

@Value("classpath:application.properties")
private Resource resourceFile;

public void test(){
// 如果文件存在,就输出文件名称
if(resourceFile.exists()){
System.out.println(resourceFile.getFilename());
}
}

}

三、使用 @ConfigurationProperties 读取配置

1、@ConfigurationProperties 读取配置参数到 String 类型

application.properties 配置文件内容:

1
my.name=mydlq

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
@ConfigurationProperties(prefix = "my") //配置 prefix 来过滤对应前缀
public class ConfigurationReadConfig {

private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

注意:使用 @ConfigurationProperties 注解读取配置,则需要配置文件内容中的参数添加统一的前缀,在 @ConfigurationProperties 注解中配置该前缀的值,然后前缀后的属性名要与加 @ConfigurationProperties 注解的类中成员变量名称保持一致。

2、@ConfigurationProperties 读取 List 类型参数

application.properties 配置文件内容:

1
2
3
my.list[0]=a
my.list[1]=b
my.list[2]=c

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfig {

private List<String> list;

public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}

}

3、@ConfigurationProperties 读取 Map 类型参数

application.properties 配置文件内容:

1
2
3
my.map.name=xiao-li
my.map.sex=man
my.map.age=20

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfig {

private Map<String, String> map;

public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}

}

4、@ConfigurationProperties 读取 Time 类型参数

application.properties 配置文件内容:

1
my.time=20s

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfig {

/**
* 设置以秒为单位
*/
@DurationUnit(ChronoUnit.SECONDS)
private Duration time;

public Duration getTime() {
return time;
}
public void setTime(Duration time) {
this.time = time;
}

}

5、@ConfigurationProperties 读取 DataSize 类型参数

application.properties 配置文件内容:

1
my.fileSize=10MB

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfig {

/**
* 设置以 MB 为单位
*/
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize fileSize;

public DataSize getFileSize() {
return fileSize;
}
public void setFileSize(DataSize fileSize) {
this.fileSize = fileSize;
}

}

6、@ConfigurationProperties 读取 DataSize 类型参数

application.properties 配置文件内容:

1
my.fileSize=10MB

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfig {

/**
* 设置以 MB 为单位
*/
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize fileSize;

public DataSize getFileSize() {
return fileSize;
}
public void setFileSize(DataSize fileSize) {
this.fileSize = fileSize;
}

}

7、@ConfigurationProperties 读取配置参数并进行 Valid 效验

application.properties 配置文件内容:

1
2
my.name=xiao-ming
my.age=20

使用 @ConfigurationProperties 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Validated  // 引入效验注解
@Configuration
@ConfigurationProperties(prefix = "my")
public class ConfigurationReadConfigAndValid {

@NotNull(message = "姓名不能为空")
private String name;
@Max(value = 20L,message = "年龄不能超过 20 岁")
private Integer age;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}

}

8、@ConfigurationProperties 读取配置到新建 Spring Bean 中

application.properties 配置文件内容:

1
2
user.name=mydlq
user.age=22

User 实体类

1
2
3
4
5
6
7
import lombok.Data;

@Data
public class User {
private String name;
private Integer age;
}

使用 @ConfigurationProperties 注解读取对应配置到新建的 Bean 对象中

1
2
3
4
5
6
7
8
9
10
@Configuration
public class ConfigurationReadObject {

@Bean("user")
@ConfigurationProperties(prefix = "user")
public User createUser(){
return new User();
}

}

9、从指定配置文件中读取参数

使用 @ConfigurationProperties 注解是默认从 application.properties 或者 application.yaml 中读取配置,有时候我们需要将特定的配置放到单独的配置文件中,这时候需要 @PropertySource 与 ConfigurationProperties 配置使用,使用 @PropertySource 注解指定要读取的文件,使用 @ConfigurationProperties 相关属性。

测试文件:

  • 测试文件名称:test.txt
  • 测试文件编码方式:UTF-8
  • 测试文件目录:resources/test.txt
  • 测试文件内容:
1
my.name=mydlq

Java 中配置 @ConfigurationProperties 和 @PropertySource 注解读取对应配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@ConfigurationProperties(prefix = "my")
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:test.txt")
public class ConfigurationReadConfig {

private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

四、使用 Environment 对象读取配置

application.properties 配置文件内容:

1
my.name=mydlq

使用 Environment 读取配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component
public class EnvironmentReadConfig {

private String name;

@Autowired
private Environment environment;

public String readConfig(){
name = environment.getProperty("my.name", "默认值");
}

}

五、使用 PropertiesLoaderUtils 读取配置

application.properties 配置文件内容:

1
my.name=mydlq

使用 Environment 读取配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class PropertiesReadConfig {

private String name;

public void readConfig() {
try {
ClassPathResource resource = new ClassPathResource("application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
name = properties.getProperty("my.name", "默认值");
} catch (IOException e) {
log.error("", e);
}
}

}

六、读取配置文件示例项目

1、Maven 引入相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>

<groupId>mydlq.club</groupId>
<artifactId>springboot-read-config-example</artifactId>
<version>0.0.1</version>
<name>springboot-read-config-example</name>
<description>Spring Boot Read Config</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--SpringBoot Web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--用于 SpringBoot 生成配置 metadata 文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--引入 Lombok 插件,方便操作实体对象-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

2、测试的配置文件

application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## 使用多种方法读取 String 参数
my1.name=xiao-ming
my1.sex=man
my1.age=20

## 使用 @ConfigurationProperties 读取 Map 参数
my2.map.name=xiao-li
my2.map.sex=man
my2.map.age=20

## 使用 @Value 读取 Map 参数
my3.map={name:"xiao-ming",sex:"man",age:"20"}

## 使用 @ConfigurationProperties 读取 List 参数
my4.list[0]=xiao-li
my4.list[1]=man
my4.list[2]=20

## 使用 @Value 读取 List 参数
my5.list=xiao-ming,man,20

## 使用 @ConfigurationProperties 读取 Time 参数
my6.time=20s

## 使用 @ConfigurationProperties 读取 DataSize 参数
my7.fileSize=10MB

## 使用 @ConfigurationProperties 读取参数并进行 @Valid 效验
my8.name=xiao-ming
my8.age=20

3、读取配置的多种方法

(1)、读取 String 配置的 Service

ConfigurationReadString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
* 通过 @PropertySource 指定读取的文件中 String 配置,通过 @ConfigurationProperties 过滤前缀
*/
@Data
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my1")
public class ConfigurationReadString {

private String name;
private String sex;
private String age;

public String readString(){
return name + "," + sex + "," + age;
}

}

EnvironmentReadString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

/**
* 从环境对象 Environment 中读取 String 配置
*/
@Service
public class EnvironmentReadString {

@Autowired
private Environment environment;

public String readString(){
String name = environment.getProperty("my1.name", "");
String sex = environment.getProperty("my1.sex", "");
String age = environment.getProperty("my1.age", "18");
return name + "," + sex + "," + age;
}

}

PropertiesUtilReadString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.IOException;
import java.util.Properties;

/**
* 通过 properties 工具读取 String 配置
*/
@Slf4j
public class PropertiesUtilReadString {

private PropertiesUtilReadString(){}

public static String readString() {
try {
ClassPathResource resource = new ClassPathResource("application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
String name = properties.getProperty("my1.name", "");
String sex = properties.getProperty("my1.sex", "");
String age = properties.getProperty("my1.age", "18");
return name + "," + sex + "," + age;
} catch (IOException e) {
log.error("", e);
}
return "";
}

}

ValueReadString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* 通过 @Value 读取 String 配置
*/
@Service
public class ValueReadString {

@Value("${my1.name}")
private String name;

@Value("${my1.sex}")
private String sex;

@Value("${my1.age:18}")
private String age;

public String readString() {
return name + "," + sex + "," + age;
}

}

(2)、读取 List 配置的 Service

ConfigurationReadList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.List;

/**
* 通过 @ConfigurationProperties 方式读取文件中的 List 数据
*/
@Data
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my4")
public class ConfigurationReadList {

private List<String> list;

public String readList() {
StringBuilder builder = new StringBuilder();
for (String str:list){
builder.append(str).append(",");
}
// 移除最后的“,”号
builder.delete(builder.length()-1,builder.length());
return builder.toString();
}

}

ValueReadList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* 通过 @Value 方式读取文件中的 List 数据
*/
@Service
public class ValueReadList {

@Value("#{'${my5.list}'.split(',')}")
private List<String> list;

public String readList() {
StringBuilder builder = new StringBuilder();
for (String str:list){
builder.append(str).append(",");
}
// 移除最后的“,”号
builder.delete(builder.length()-1,builder.length());
return builder.toString();
}

}

(3)、读取 Map 配置的 Service

ConfigurationReadMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.Map;

/**
* 通过 @ConfigurationProperties 方式读取文件中的 Map 数据
*/
@Data
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my2")
public class ConfigurationReadMap {

private Map<String,String> map;

}

ValueReadMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Map;

/**
* 通过 @Value 方式读取文件中的 Map 数据
*/
@Service
public class ValueReadMap {

@Value("#{${my3.map}}")
private Map<String, String> map;

public String readMap(){
return map.get("name") + "," + map.get("sex") + "," + map.get("age");
}

}

(4)、读取 Time 的 Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

/**
* 通过 @ConfigurationProperties 读取 time 参数
*/
@Data
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my6")
public class ConfigurationReadTime {

/**
* 设置以秒为单位
*/
@DurationUnit(ChronoUnit.SECONDS)
private Duration time;

public String readTime() {
return String.valueOf(time.getSeconds());
}

}

常见单位如下:

B for bytes KB for kilobytes MB for megabytes GB for gigabytes TB for terabytes

(5)、读取 DataSize 的 Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;

/**
* 通过 @ConfigurationProperties 读取 time 参数
*/
@Data
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my7")
public class ConfigurationReadDatasize {

/**
* 设置以秒为单位
*/
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize fileSize;

public String readDatasize() {
return String.valueOf(fileSize.toMegabytes());
}

}

常用单位如下:

  • ns (纳秒)
  • us (微秒)
  • ms (毫秒)
  • s (秒)
  • m (分)
  • h (时)
  • d (天)

(6)、读取配置并进行 valie 效验的 service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;

/**
* 通过 @ConfigurationProperties 读取配置并进行 valid 效验
*/
@Data
@Validated // 效验注解
@Configuration
@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
@ConfigurationProperties(prefix = "my8")
public class ConfigurationReadConfigAndValid {

@NotNull(message = "姓名不能为空")
private String name;
@Max(value = 20L,message = "年龄不能超过 20 岁")
private Integer age;

public String readString() {
return name + "," + age;
}

}

(7)、读取配置到 Spring Bean 对象中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* 通过 @ConfigurationProperties 读取到 Spring Bean 对象中
*/
@Configuration
public class ConfigurationReadObject {

/**
* 测试的实体类对象
*/
@Data
public static class User {
private String name;
private Integer age;
}

/**
* 读取以 my9 为前缀的参数的值,到新建的对象中
*/
@Bean("user")
@ConfigurationProperties(prefix = "my9")
public User readObjectData(){
return new User();
}

}

4、配置测试用的 Controller

(1)、读取 String 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import club.mydlq.service.string.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取 String Controller
*/
@RestController
@RequestMapping("/string")
public class ReadStringController {

@Autowired
private ValueReadString valueReadConfig;

@Autowired
private EnvironmentReadString environmentReadConfig;

@Autowired
private ConfigurationReadString configurationReadConfig;

@GetMapping("/value")
public String valueReadConfig(){
return valueReadConfig.readString();
}

@GetMapping("/env")
public String envReadConfig(){
return environmentReadConfig.readString();
}

@GetMapping("/util")
public String utilReadConfig(){
return PropertiesUtilReadString.readString();
}

@GetMapping("/configuration")
public String configurationReadConfig(){
return configurationReadConfig.readString();
}

}

(2)、读取 List 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import club.mydlq.service.list.ConfigurationReadList;
import club.mydlq.service.list.ValueReadList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取 List Controller
*/
@RestController
@RequestMapping("/list")
public class ReadListController {

@Autowired
private ValueReadList vlueReadList;

@Autowired
private ConfigurationReadList configurationReadList;

@GetMapping("/value")
public String valueReadList(){
return vlueReadList.readList();
}

@GetMapping("/configuration")
public String configReadList(){
return configurationReadList.readList();
}

}

(3)、读取 Map 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import club.mydlq.service.map.ConfigurationReadMap;
import club.mydlq.service.map.ValueReadMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取 Map Controller
*/
@RestController
@RequestMapping("/map")
public class ReadMapController {

@Autowired
private ConfigurationReadMap configurationReadMap;

@Autowired
private ValueReadMap valueReadMap;

@GetMapping("/configuration")
public String configReadMap(){
return configurationReadMap.getMap().get("name") + "," + configurationReadMap.getMap().get("sex") + "," + configurationReadMap.getMap().get("age");
}

@GetMapping("/value")
public String valueReadMap(){
return valueReadMap.readMap();
}

}

(4)、读取 Time 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import club.mydlq.service.time.ConfigurationReadTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取 time Controller
*/
@RestController
@RequestMapping("/time")
public class ReadTimeController {

@Autowired
private ConfigurationReadTime configurationReadTime;

@GetMapping("/configuration")
public String configReadList(){
return configurationReadTime.readTime();
}

}

(5)、读取 Datasize 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import club.mydlq.service.datasize.ConfigurationReadDatasize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取 Datasize Controller
*/
@RestController
@RequestMapping("/data")
public class ReadDatasizeController {

@Autowired
private ConfigurationReadDatasize configurationReadDatasize;

@GetMapping("/configuration")
public String configReadList(){
return configurationReadDatasize.readDatasize();
}

}

(6)、读取 Datasize 的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import club.mydlq.service.valid.ConfigurationReadConfigAndValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取参数进行 valid 效验的 Controller
*/
@RestController
@RequestMapping("/valid")
public class ReadValidController {

@Autowired
private ConfigurationReadConfigAndValid configurationReadConfigAndValid;

@GetMapping("/configuration")
public String configReadList(){
return configurationReadConfigAndValid.readString();
}

}

(7)、读取参数到 Spring Bean 对象的 Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import club.mydlq.service.string.ConfigurationReadObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* 读取参数到对象
*/
@RestController
@RequestMapping("/object")
public class ReadObjectController {

@Autowired
private ConfigurationReadObject.User user;

@GetMapping("/value")
public Object valueReadConfig(){
return user;
}

}

5、启动类

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

转自:http://www.mydlq.club/article/61/