查看原文
其他

SpringBoot 多种读取配置文件中参数的方式

超级小豆丁 SpringForAll社区 2021-05-26
点击上方☝SpringForAll社区 轻松关注!
及时获取有趣有料的技术文章

本文来源:http://www.mydlq.club/article/61/


. 一、简介

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

. 2、@Value 和 @ConfigurationProperties 的区别

. 二、使用 @Value 读取配置

. 1、@Value 读取配置参数

. 2、@Value 给参数设定值

. 3、@Value 读取系统属性

. 4、@Value 读取 Bean 的属性

. 5、@Value 使用 SpEL 表达式

. 6、@Value 读取 Resource 资源文件

. 三、使用 @ConfigurationProperties 读取配置

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

. 2、@ConfigurationProperties 读取 List 类型参数

. 3、@ConfigurationProperties 读取 Map 类型参数

. 4、@ConfigurationProperties 读取 Time 类型参数

. 5、@ConfigurationProperties 读取 DataSize 类型参数

. 6、@ConfigurationProperties 读取 DataSize 类型参数

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

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

. 四、使用 Environment 对象读取配置

. 五、使用 PropertiesLoaderUtils 读取配置

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

. 1、Maven 引入相关依赖

. 2、测试的配置文件

. 3、读取配置的多种方法

. 4、配置测试用的 Controller

. 5、启动类


系统环境:

  • SpringBoot 版本:2.2.2

参考地址:

  • @ConfigurationProperties 注解使用姿势,这一篇就够了

  • 示例项目 Github 地址:https://github.com/my-dlq/blog-example/tree/master/springboot/springboot-read-config-example

一、简介

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

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

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

  • 使用 @Value 注解读取配置

  • 使用 @ConfigurationProperties 注解读取配置

  • 使用 Environment 对象读取配置

  • 使用 PropertiesLoaderUtils 工具读取配置

2、@Value 和 @ConfigurationProperties 的区别

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

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

二、使用 @Value 读取配置

1、@Value 读取配置参数

application.properties 配置文件内容:

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

使用 @Value 读取配置文件

1@Component
2public class ReadProperties {
3
4    @Value("${my.name}")
5    private String name;
6
7    @Value("${my.age}")
8    private Integer age;
9
10}

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

1通过 @Value 读取配置文件
2@Component
3public class ReadProperties {
4
5    @Value("${my.name:默认姓名}")
6    private String name;
7
8    @Value("${my.age:18}")
9    private Integer age;
10
11}

2、@Value 给参数设定值

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

1@Component
2public class ReadProperties {
3
4    @Value("#{'test value'}")
5    private String value;
6
7}

3、@Value 读取系统属性

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

1@Component
2public class ReadProperties {
3
4    @Value("#{systemProperties['os.name']}")
5    private String systemPropertiesName;
6
7}

4、@Value 读取 Bean 的属性

测试用的实体对象:

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

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

1@Component
2public class ReadProperties {
3
4    @Bean
5    public User user(){
6        User user = new User();
7        user.setName("测试");
8        user.setAge("18");
9        return user;
10    }
11
12    @Value("#{user.name}")
13    private String value;
14
15}

5、@Value 使用 SpEL 表达式

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

1@Component
2public class ReadProperties {
3
4    @Value("#{ T(java.lang.Math).random() * 100.0 }")
5    private double random;
6
7}

6、@Value 读取 Resource 资源文件

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

1@Component
2public class ReadProperties {
3
4    @Value("classpath:application.properties")
5    private Resource resourceFile;
6
7    public void test(){
8        // 如果文件存在,就输出文件名称
9        if(resourceFile.exists()){
10            System.out.println(resourceFile.getFilename());
11        }
12    }
13
14}

三、使用 @ConfigurationProperties 读取配置

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

application.properties 配置文件内容:

1my.name=mydlq

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

1@Configuration
2@ConfigurationProperties(prefix = "my")   //配置 prefix 来过滤对应前缀
3public class ConfigurationReadConfig {
4
5    private String name;
6
7    public String getName() {
8        return name;
9    }
10    public void setName(String name) {
11        this.name = name;
12    }
13
14}

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

2、@ConfigurationProperties 读取 List 类型参数

application.properties 配置文件内容:

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

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

1@Configuration
2@ConfigurationProperties(prefix = "my")
3public class ConfigurationReadConfig {
4
5    private List<String> list;
6
7    public List<String> getList() {
8        return list;
9    }
10    public void setList(List<String> list) {
11        this.list = list;
12    }
13
14}

3、@ConfigurationProperties 读取 Map 类型参数

application.properties 配置文件内容:

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

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

1@Configuration
2@ConfigurationProperties(prefix = "my")
3public class ConfigurationReadConfig {
4
5    private Map<String, String> map;
6
7    public Map<String, String> getMap() {
8        return map;
9    }
10    public void setMap(Map<String, String> map) {
11        this.map = map;
12    }
13
14}

4、@ConfigurationProperties 读取 Time 类型参数

application.properties 配置文件内容:

1my.time=20s

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

1@Configuration
2@ConfigurationProperties(prefix = "my")
3public class ConfigurationReadConfig {
4
5     /**
6     * 设置以秒为单位
7     */

8    @DurationUnit(ChronoUnit.SECONDS)
9    private Duration time;
10
11    public Duration getTime() {
12        return time;
13    }
14    public void setTime(Duration time) {
15        this.time = time;
16    }
17
18}

5、@ConfigurationProperties 读取 DataSize 类型参数

application.properties 配置文件内容:

1my.fileSize=10MB

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

1@Configuration
2@ConfigurationProperties(prefix = "my")
3public class ConfigurationReadConfig {
4
5    /**
6     * 设置以 MB 为单位
7     */

8    @DataSizeUnit(DataUnit.MEGABYTES)
9    private DataSize fileSize;
10
11    public DataSize getFileSize() {
12        return fileSize;
13    }
14    public void setFileSize(DataSize fileSize) {
15        this.fileSize = fileSize;
16    }
17
18}

6、@ConfigurationProperties 读取 DataSize 类型参数

application.properties 配置文件内容:

1my.fileSize=10MB

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

1@Configuration
2@ConfigurationProperties(prefix = "my")
3public class ConfigurationReadConfig {
4
5    /**
6     * 设置以 MB 为单位
7     */

8    @DataSizeUnit(DataUnit.MEGABYTES)
9    private DataSize fileSize;
10
11    public DataSize getFileSize() {
12        return fileSize;
13    }
14    public void setFileSize(DataSize fileSize) {
15        this.fileSize = fileSize;
16    }
17
18}

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

application.properties 配置文件内容:

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

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

1@Validated  // 引入效验注解
2@Configuration
3@ConfigurationProperties(prefix = "my")
4public class ConfigurationReadConfigAndValid {
5
6    @NotNull(message = "姓名不能为空")
7    private String name;
8    @Max(value = 20L,message = "年龄不能超过 20 岁")
9    private Integer age;
10
11    public String getName() {
12        return name;
13    }
14    public void setName(String name) {
15        this.name = name;
16    }
17    public Integer getAge() {
18        return age;
19    }
20    public void setAge(Integer age) {
21        this.age = age;
22    }
23
24}

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

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

测试文件:

  • 测试文件名称:test.txt

  • 测试文件编码方式:UTF-8

  • 测试文件目录:resources/test.txt

  • 测试文件内容:

1my.name=mydlq

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

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

四、使用 Environment 对象读取配置

application.properties 配置文件内容:

1my.name=mydlq

使用 Environment 读取配置:

1@Component
2public class EnvironmentReadConfig {
3
4    private String name;
5
6    @Autowired
7    private Environment environment;
8
9    public String readConfig(){
10        name = environment.getProperty("my.name""默认值");
11    }
12
13}

五、使用 PropertiesLoaderUtils 读取配置

application.properties 配置文件内容:

1my.name=mydlq

使用 Environment 读取配置:

1public class PropertiesReadConfig {
2
3    private String name;
4
5    public void readConfig() {
6        try {
7            ClassPathResource resource = new ClassPathResource("application.properties");
8            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
9            name = properties.getProperty("my.name""默认值");
10        } catch (IOException e) {
11            log.error("", e);
12        }
13    }
14
15}

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

1、Maven 引入相关依赖

1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4    <modelVersion>4.0.0</modelVersion>
5
6    <parent>
7        <groupId>org.springframework.boot</groupId>
8        <artifactId>spring-boot-starter-parent</artifactId>
9        <version>2.2.2.RELEASE</version>
10    </parent>
11
12    <groupId>com.aspirecn</groupId>
13    <artifactId>springboot-read-config-example</artifactId>
14    <version>0.0.1</version>
15    <name>springboot-read-config-example</name>
16    <description>Spring Boot Read Config</description>
17
18    <properties>
19        <java.version>1.8</java.version>
20    </properties>
21
22    <dependencies>
23        <!--SpringBoot Web 依赖-->
24        <dependency>
25            <groupId>org.springframework.boot</groupId>
26            <artifactId>spring-boot-starter-web</artifactId>
27        </dependency>
28        <!--用于 SpringBoot 生成配置 metadata 文件-->
29        <dependency>
30            <groupId>org.springframework.boot</groupId>
31            <artifactId>spring-boot-configuration-processor</artifactId>
32            <optional>true</optional>
33        </dependency>
34        <!--引入 Lombok 插件,方便操作实体对象-->
35        <dependency>
36            <groupId>org.projectlombok</groupId>
37            <artifactId>lombok</artifactId>
38        </dependency>
39    </dependencies>
40
41    <build>
42        <plugins>
43            <plugin>
44                <groupId>org.springframework.boot</groupId>
45                <artifactId>spring-boot-maven-plugin</artifactId>
46            </plugin>
47        </plugins>
48    </build>
49
50</project>

2、测试的配置文件

application.properties

1## 使用多种方法读取 String 参数
2my1.name=xiao-ming
3my1.sex=man
4my1.age=20
5
6## 使用 @ConfigurationProperties 读取 Map 参数
7my2.map.name=xiao-li
8my2.map.sex=man
9my2.map.age=20
10
11## 使用 @Value 读取 Map 参数
12my3.map={name:"xiao-ming",sex:"man",age:"20"}
13
14## 使用 @ConfigurationProperties 读取 List 参数
15my4.list[0]=xiao-li
16my4.list[1]=man
17my4.list[2]=20
18
19## 使用 @Value 读取 List 参数
20my5.list=xiao-ming,man,20
21
22## 使用 @ConfigurationProperties 读取 Time 参数
23my6.time=20s
24
25## 使用 @ConfigurationProperties 读取 DataSize 参数
26my7.fileSize=10MB
27
28## 使用 @ConfigurationProperties 读取参数并进行 @Valid 效验
29my8.name=xiao-ming
30my8.age=20

3、读取配置的多种方法

(1)、读取 String 配置的 Service

ConfigurationReadString

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.context.annotation.PropertySource;
5
6/**
7 * 通过 @PropertySource 指定读取的文件中 String 配置,通过 @ConfigurationProperties 过滤前缀
8 */

9@Data
10@Configuration
11@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
12@ConfigurationProperties(prefix = "my1")
13public class ConfigurationReadString {
14
15    private String name;
16    private String sex;
17    private String age;
18
19    public String readString(){
20        return name + "," + sex + "," + age;
21    }
22
23}

EnvironmentReadString

1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.core.env.Environment;
3import org.springframework.stereotype.Service;
4
5/**
6 * 从环境对象 Environment 中读取 String 配置
7 */

8@Service
9public class EnvironmentReadString {
10
11    @Autowired
12    private Environment environment;
13
14    public String readString(){
15        String name = environment.getProperty("my1.name""");
16        String sex = environment.getProperty("my1.sex""");
17        String age = environment.getProperty("my1.age""18");
18        return name + "," + sex + "," + age;
19    }
20
21}

PropertiesUtilReadString

1import lombok.extern.slf4j.Slf4j;
2import org.springframework.core.io.ClassPathResource;
3import org.springframework.core.io.support.PropertiesLoaderUtils;
4import java.io.IOException;
5import java.util.Properties;
6
7/**
8 * 通过 properties 工具读取 String 配置
9 */

10@Slf4j
11public class PropertiesUtilReadString {
12
13    private PropertiesUtilReadString(){}
14
15    public static String readString() {
16        try {
17            ClassPathResource resource = new ClassPathResource("application.properties");
18            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
19            String name = properties.getProperty("my1.name""");
20            String sex = properties.getProperty("my1.sex""");
21            String age = properties.getProperty("my1.age""18");
22            return name + "," + sex + "," + age;
23        } catch (IOException e) {
24            log.error("", e);
25        }
26        return "";
27    }
28
29}

ValueReadString

1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Service;
3
4/**
5 * 通过 @Value 读取 String 配置
6 */

7@Service
8public class ValueReadString {
9
10    @Value("${my1.name}")
11    private String name;
12
13    @Value("${my1.sex}")
14    private String sex;
15
16    @Value("${my1.age:18}")
17    private String age;
18
19    public String readString() {
20        return name + "," + sex + "," + age;
21    }
22
23}

(2)、读取 List 配置的 Service

ConfigurationReadList

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.context.annotation.PropertySource;
5import java.util.List;
6
7/**
8 * 通过 @ConfigurationProperties 方式读取文件中的 List 数据
9 */

10@Data
11@Configuration
12@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
13@ConfigurationProperties(prefix = "my4")
14public class ConfigurationReadList {
15
16    private List<String> list;
17
18    public String readList() {
19        StringBuilder builder = new StringBuilder();
20        for (String str:list){
21            builder.append(str).append(",");
22        }
23        // 移除最后的“,”号
24        builder.delete(builder.length()-1,builder.length());
25        return builder.toString();
26    }
27
28}

ValueReadList

1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Service;
3import java.util.List;
4
5/**
6 * 通过 @Value 方式读取文件中的 List 数据
7 */

8@Service
9public class ValueReadList {
10
11    @Value("#{'${my5.list}'.split(',')}")
12    private List<String> list;
13
14    public String readList() {
15        StringBuilder builder = new StringBuilder();
16        for (String str:list){
17            builder.append(str).append(",");
18        }
19        // 移除最后的“,”号
20        builder.delete(builder.length()-1,builder.length());
21        return builder.toString();
22    }
23
24}

(3)、读取 Map 配置的 Service

ConfigurationReadMap

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.context.annotation.PropertySource;
5import java.util.Map;
6
7/**
8 * 通过 @ConfigurationProperties 方式读取文件中的 Map 数据
9 */

10@Data
11@Configuration
12@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
13@ConfigurationProperties(prefix = "my2")
14public class ConfigurationReadMap {
15
16    private Map<String,String> map;
17
18}

ValueReadMap

1import org.springframework.beans.factory.annotation.Value;
2import org.springframework.stereotype.Service;
3import java.util.Map;
4
5/**
6 * 通过 @Value 方式读取文件中的 Map 数据
7 */

8@Service
9public class ValueReadMap {
10
11    @Value("#{${my3.map}}")
12    private Map<String, String> map;
13
14    public String readMap(){
15        return map.get("name") + "," + map.get("sex") + "," + map.get("age");
16    }
17
18}

(4)、读取 Time 的 Service

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.boot.convert.DurationUnit;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.context.annotation.PropertySource;
6import java.time.Duration;
7import java.time.temporal.ChronoUnit;
8
9/**
10 * 通过 @ConfigurationProperties 读取 time 参数
11 */

12@Data
13@Configuration
14@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
15@ConfigurationProperties(prefix = "my6")
16public class ConfigurationReadTime {
17
18    /**
19     * 设置以秒为单位
20     */

21    @DurationUnit(ChronoUnit.SECONDS)
22    private Duration time;
23
24    public String readTime() {
25        return String.valueOf(time.getSeconds());
26    }
27
28}

常见单位如下:

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

(5)、读取 DataSize 的 Service

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.boot.convert.DataSizeUnit;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.context.annotation.PropertySource;
6import org.springframework.util.unit.DataSize;
7import org.springframework.util.unit.DataUnit;
8
9/**
10 * 通过 @ConfigurationProperties 读取 time 参数
11 */

12@Data
13@Configuration
14@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
15@ConfigurationProperties(prefix = "my7")
16public class ConfigurationReadDatasize {
17
18    /**
19     * 设置以秒为单位
20     */

21    @DataSizeUnit(DataUnit.MEGABYTES)
22    private DataSize fileSize;
23
24    public String readDatasize() {
25        return String.valueOf(fileSize.toMegabytes());
26    }
27
28}

常用单位如下:

  • ns (纳秒)

  • us (微秒)

  • ms (毫秒)

  • s (秒)

  • m (分)

  • h (时)

  • d (天)

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

1import lombok.Data;
2import org.springframework.boot.context.properties.ConfigurationProperties;
3import org.springframework.context.annotation.Configuration;
4import org.springframework.context.annotation.PropertySource;
5import org.springframework.validation.annotation.Validated;
6import javax.validation.constraints.Max;
7import javax.validation.constraints.NotNull;
8
9/**
10 * 通过 @ConfigurationProperties 读取配置并进行 valid 效验
11 */

12@Data
13@Validated  // 效验注解
14@Configuration
15@PropertySource(encoding = "UTF-8", ignoreResourceNotFound = true, value = "classpath:application.properties")
16@ConfigurationProperties(prefix = "my8")
17public class ConfigurationReadConfigAndValid {
18
19    @NotNull(message = "姓名不能为空")
20    private String name;
21    @Max(value = 20L,message = "年龄不能超过 20 岁")
22    private Integer age;
23
24    public String readString() {
25        return name + "," + age;
26    }
27
28}

4、配置测试用的 Controller

(1)、读取 String 的 Controller

1import club.mydlq.service.string.*;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7/**
8 * 读取 String Controller
9 */

10@RestController
11@RequestMapping("/string")
12public class ReadStringController {
13
14    @Autowired
15    private ValueReadString valueReadConfig;
16
17    @Autowired
18    private EnvironmentReadString environmentReadConfig;
19
20    @Autowired
21    private ConfigurationReadString configurationReadConfig;
22
23    @GetMapping("/value")
24    public String valueReadConfig(){
25        return valueReadConfig.readString();
26    }
27
28    @GetMapping("/env")
29    public String envReadConfig(){
30        return environmentReadConfig.readString();
31    }
32
33    @GetMapping("/util")
34    public String utilReadConfig(){
35        return PropertiesUtilReadString.readString();
36    }
37
38    @GetMapping("/configuration")
39    public String configurationReadConfig(){
40        return configurationReadConfig.readString();
41    }
42
43}

(2)、读取 List 的 Controller

1import club.mydlq.service.list.ConfigurationReadList;
2import club.mydlq.service.list.ValueReadList;
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.web.bind.annotation.GetMapping;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RestController;
7
8/**
9 * 读取 List Controller
10 */

11@RestController
12@RequestMapping("/list")
13public class ReadListController {
14
15    @Autowired
16    private ValueReadList vlueReadList;
17
18    @Autowired
19    private ConfigurationReadList configurationReadList;
20
21    @GetMapping("/value")
22    public String valueReadList(){
23        return vlueReadList.readList();
24    }
25
26    @GetMapping("/configuration")
27    public String configReadList(){
28        return configurationReadList.readList();
29    }
30
31}

(3)、读取 Map 的 Controller

1import club.mydlq.service.map.ConfigurationReadMap;
2import club.mydlq.service.map.ValueReadMap;
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.web.bind.annotation.GetMapping;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RestController;
7
8/**
9 * 读取 Map Controller
10 */

11@RestController
12@RequestMapping("/map")
13public class ReadMapController {
14
15    @Autowired
16    private ConfigurationReadMap configurationReadMap;
17
18    @Autowired
19    private ValueReadMap valueReadMap;
20
21    @GetMapping("/configuration")
22    public String configReadMap(){
23        return configurationReadMap.getMap().get("name") + "," + configurationReadMap.getMap().get("sex") + "," + configurationReadMap.getMap().get("age");
24    }
25
26    @GetMapping("/value")
27    public String valueReadMap(){
28        return valueReadMap.readMap();
29    }
30
31}

(4)、读取 Time 的 Controller

1import club.mydlq.service.time.ConfigurationReadTime;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7/**
8 * 读取 time Controller
9 */

10@RestController
11@RequestMapping("/time")
12public class ReadTimeController {
13
14    @Autowired
15    private ConfigurationReadTime configurationReadTime;
16
17    @GetMapping("/configuration")
18    public String configReadList(){
19        return configurationReadTime.readTime();
20    }
21
22}

(5)、读取 Datasize 的 Controller

1import club.mydlq.service.datasize.ConfigurationReadDatasize;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7/**
8 * 读取 Datasize Controller
9 */

10@RestController
11@RequestMapping("/data")
12public class ReadDatasizeController {
13
14    @Autowired
15    private ConfigurationReadDatasize configurationReadDatasize;
16
17    @GetMapping("/configuration")
18    public String configReadList(){
19        return configurationReadDatasize.readDatasize();
20    }
21
22}

(6)、读取 Datasize 的 Controller

1import club.mydlq.service.valid.ConfigurationReadConfigAndValid;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7/**
8 * 读取参数进行 valid 效验的 Controller
9 */

10@RestController
11@RequestMapping("/valid")
12public class ReadValidController {
13
14    @Autowired
15    private ConfigurationReadConfigAndValid configurationReadConfigAndValid;
16
17    @GetMapping("/configuration")
18    public String configReadList(){
19        return configurationReadConfigAndValid.readString();
20    }
21
22}

5、启动类

1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3
4@SpringBootApplication
5public class Application {
6
7    public static void main(String[] args) {
8        SpringApplication.run(Application.class, args);
9    }
10
11}





● SpringBoot 操作 ElasticSearch 详解

● SpringBoot 使用 Caffeine 本地缓存

● Github推出了GitHub CLI

● (很全面)SpringBoot 集成 Apollo 配置中心

● 你知道如何成为一名靠谱的架构师不?

● Tomcat 在 SpringBoot 中是如何启动的?

● SpringBoot 深度调优,让你的项目飞起来!

● 8种经常被忽视的SQL错误用法,你有没有踩过坑?

● Java面试应该知道之深入理解Java的接口和抽象类

● Spring系列之beanFactory与ApplicationContext

● 多线程同步的五种方法

● redis应用场景

● 手把手带你剖析 Springboot 启动原理!

● Java多线程:synchronized关键字和Lock

● Java多线程:多线程基础知识

● Kafka基本架构及原理

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存