首页 > 编程语言 > @ConfigurationProperties绑定配置信息至Array、List、Map、Bean的实现
2020
09-29

@ConfigurationProperties绑定配置信息至Array、List、Map、Bean的实现

相关说明:

在SpringBoot中,我们可以通过以下几种方式获取并绑定配置文件中的信息:

  1. @Value注解。
  2. 使用Environment。
  3. @ConfigurationProperties注解。
  4. 通过实现ApplicationListener接口,注册监听器,进行硬编码获取,可参考:https://www.jb51.net/article/187407.htm
  5. 硬编码加载文件获取。
  6. ……

注:一般情况下,第一种、第二种就够用了;但是如果想直接从配置文件中获取到数组、list、map、对象的话,
第一种和第二种的支持性不太好,目前只能获取到数组、list,对map、bean的话,就有点束手无策了。
这时我们可以使用第三种方式进行获取。

环境说明:Windows10、IntelliJ IDEA、SpringBoot 2.1.5.RELEASE

@ConfigurationProperties注解获取配置信息并绑定到对象上示例:

准备工作:引入spring-boot-configuration-processor依赖

给出本人完整的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 http://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.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.aspire.yaml-properties</groupId>
  <artifactId>yaml-properties</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>yaml文件与properties文件语法示例</name>
  <description>yaml文件与properties文件语法示例</description>
 
  <properties>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
 
    <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-configuration-processor</artifactId>
    </dependency>
  </dependencies>
 
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
 
</project>

加载demo.properties文件中的信息绑定到bean,并注入容器示例:

项目结构说明:

注:application.yml其实在本次演示中,没有用到。不过本人给出了如何在yml文件中进行类似配置的
示例(见本文末尾)。
追注:如果要读取的是yml或yaml中的文件,那么最好是直接写在application.yml或
application.yaml文件中,写在其他yml文件中的话,可能导致读取失败。
(P.S.如果是写在其他yml或yaml文件中的话,只能通过最后一级key进行
定位,通过多级key可能定位不到。)

先给出一个等下会用到的Uer类:

注:上图中的注解是快速开发lombok注解,也可以不使用这些注解,自己写getter、setter等方法。

DemoConfig中是这样的:

注:上图中的@Data注解属于快速开发lombok注解,该注解不是必须的,也可以不要该注解,自己手写
getter、setter、toString等。

deme.properties中是这样的:

测试一下:

运行测试方法,控制台输出(为了方便观察,本人手动换行整理了一下输出的内容):

由此可见,成功将demo.peoperties文件中的信息绑定到bean上,并注入Spring容器成功!

拓展:

如果是application.yml(或application.yaml)文件,类似的,我们可以这么配置:

参考链接
https://www.jb51.net/article/187407.htm

测试代码托管链接
https://github.com/JustryDeng/CommonRepository/tree/master

到此这篇关于@ConfigurationProperties绑定配置信息至Array、List、Map、Bean的实现的文章就介绍到这了,更多相关@ConfigurationProperties绑定至Array、List、Map、Bean内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧