Java中@ConfigurationProperties实现自定义配置绑定问题分析

作者:Acelin_H''s Blog 时间:2023-01-23 23:23:47 

目录
  • @ConfigurationProperties使用

  • @ConfigurationProperties特点

    • 宽松绑定

    • 支持复杂属性类型

  • 激活@ConfigurationProperties

    • 通过@EnableConfigurationProperties

    • 通过@ConfigurationPropertiesScan

  • @ConfigurationProperties与@Value对比

    • 使用 Spring Boot Configuration Processor 完成自动补全

      @ConfigurationProperties使用

      创建一个类,类名上方注解,配置prefix属性,如下代码:


      @ConfigurationProperties(
             prefix = "hello.properties"
      )
      public class MyProperties {

      private String myKey;
         private List<String> stringList;
         private Duration duration;

      public String getMyKey() {
             return myKey;
         }

      public void setMyKey(String myKey) {
             this.myKey = myKey;
         }

      public List<String> getStringList() {
             return stringList;
         }

      public void setStringList(List<String> stringList) {
             this.stringList = stringList;
         }

      public Duration getDuration() {
             return duration;
         }

      public void setDuration(Duration duration) {
             this.duration = duration;
         }

      @Override
         public String toString() {
             return "MyProperties{" +
                     "myKey='" + myKey + '\'' +
                     ", stringList=" + stringList +
                     ", duration=" + duration +
                     '}';
         }
      }

      prefix属性是配置文件里的前缀,即配置文件中以前缀 + 变量名的形式配置一条记录,来对应类中的一个变量,如下:


      hello.properties.myKey=hello
      hello.properties.duration=20s
      hello.properties.string-list[0]=Acelin
      hello.properties.string-list[1]=nice

      @ConfigurationProperties特点

      宽松绑定

      如下配置都是可以被识别绑定的:


      hello.properties.myKey=hello
      hello.properties.mykey=hello
      hello.properties.my-key=hello
      hello.properties.my_key=hello
      hello.properties.MY_KEY=hello
      hello.properties.MY-KEY=hello

      支持复杂属性类型

      支持从配置参数中解析 durations (持续时间)


      hello.properties.duration=20s

      List 和 Set


      hello.properties.string-list[0]=Acelin
      hello.properties.string-list[1]=nice

      激活@ConfigurationProperties

      通过@EnableConfigurationProperties

      如果一个配置类只单单用@ConfigurationProperties注解,那么在IOC容器中是获取不到properties 配置文件转化的bean。我们可以在想要使用该配置类的类上注解@EnableConfigurationProperties,并配置相关的类,即可拿到该装配好配置的类了。如下所示:

      通过@ConfigurationPropertiesScan

      该注解有点类似与@CompomentScan注解扫描@Compoment注释的类相似,也是用来扫描项目中@ConfigurationProperties注解的类,并注入spring容器中。只需将该注解注释于项目启动类上即可

      其实@ConfigurationProperties更多的作用是将配置文件中的配置与类中变量对应上来,而上述两种方式是告诉Spring容器要把这个有配置特性的Bean在程序启动的时候给创建出来。那谈到的创建Bean,我们就会想到Spring创建Bean的各种方式,这些方式的同样能够激活@ConfigurationProperties,详细请看Spring Boot创建Bean的几种方式

      @ConfigurationProperties与@Value对比


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

      使用 Spring Boot Configuration Processor 完成自动补全

      当我们在配置文件中写官方支持的配置的时候,我们都会发现的有自动补全配置的一个功能,那怎么也让我们自己的配置也实现这种功能呢?

      其实当你用这个注解的时候,IDE是会提示你这一点的,她会在文件的上方提示你要可以配置自动补全的功能:

      Java中@ConfigurationProperties实现自定义配置绑定问题分析

      实现的方式就是项目导入依赖:


      <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-configuration-processor</artifactId>
             </dependency>

      然后重新编译或运行项目:

      项目会生产一个json文件

      Java中@ConfigurationProperties实现自定义配置绑定问题分析

      然后能够实现自动提示补全配置项的功能了

      Java中@ConfigurationProperties实现自定义配置绑定问题分析

      来源:https://www.cnblogs.com/acelin/p/15167266.html

      标签:@ConfigurationProperties,自定义配置,绑定
      0
      投稿

      猜你喜欢

    • Java操作excel的三种常见方法实例

      2022-12-11 02:29:55
    • Mybatis中的like模糊查询功能

      2023-09-25 11:57:28
    • Java集合之Comparable和Comparator接口详解

      2022-10-04 06:03:44
    • Kotlin的::符号怎么用

      2022-07-13 22:59:37
    • Android Studio gradle 编译提示‘default not found’ 解决办法

      2023-07-19 09:23:06
    • MyBatis-Plus 如何单元测试的实现

      2022-03-02 19:45:08
    • C语言实例梳理讲解常用关键字的用法

      2023-04-09 01:22:52
    • Android开发签名知识梳理总结

      2023-03-15 03:52:02
    • Flutter路由传递参数及解析实现

      2023-06-22 11:48:45
    • 轻松学习C#的异常处理

      2022-09-14 22:10:20
    • Winform开发中使用下拉列表展示字典数据的几种方式

      2022-02-23 11:56:41
    • spring中bean的生命周期详解

      2021-11-29 23:31:02
    • 详解SpringBoot统一响应体解决方案

      2023-03-08 08:54:13
    • 通过实例解析Spring argNames属性

      2023-09-14 10:43:13
    • java日期时间操作工具类

      2022-09-03 13:04:58
    • Java 实现常见的非对称加密算法

      2023-11-27 18:51:03
    • docker 的java编译环境构建详细介绍

      2023-02-10 04:08:30
    • WCF实现进程间管道通信Demo分享

      2022-10-22 04:20:06
    • C#实现程序等待延迟执行的方法

      2023-07-16 06:32:36
    • Java开发中synchronized的定义及用法详解

      2021-11-11 07:01:20
    • asp之家 软件编程 m.aspxhome.com