软件编程
位置:首页>> 软件编程>> java编程>> Java中@ConfigurationProperties实现自定义配置绑定问题分析

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

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

标签:@ConfigurationProperties,自定义配置,绑定
目录
  • @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

      0
      投稿

      猜你喜欢

      手机版 软件编程 asp之家 www.aspxhome.com