• SpringBoot中@ConfigurationProperties注解的常见使用(学习笔记)


    1、@ConfigurationProperties注解的概述

      @ConfigurationProperties是springboot提供读取配置文件的一个注解。其对应的bean的后置处理器为
    ConfigurationPropertiesBindingPostProcessor类,它是实现了BeanPostProcessor接口,在bean被实例化后,会调用后置处理,递归的查找属性,通过反射注入值,对大多数属性而言强制需提供其setter和getter方法。

      @ConfigurationProperties注解加载配置文件三种常见的用法,如下:

    1. @ConfigurationProperties配合@Component注解在Bean类上使用
    2. @ConfigurationProperties配合@Bean注解在配置类的Bean定义方法上使用
    3. @ConfigurationProperties配合@EnableConfigurationProperties使用

    2、配合@Component注解在Bean类上使用

      这里需要注意的是,@ConfigurationProperties注解不仅可以配合@Component注解使用,同时还可以配合继承了@Component注解的其他注解,比如@Controller、@Service、@Repository等,当然这些个注解都有固定的使用常见,所以很少配合@ConfigurationProperties使用,但是配合@Configuration注解使用的情况还是比较常见的,而@Configuration注解也继承了@Component注解。

      具体使用方法如下,这里对应的属性值可以是简单字符串,Map对象、List对象等:

    @Slf4j
    @Getter
    @Setter
    @ConfigurationProperties(prefix = MultiDataSourceProperties.PREFIX)
    @Configuration
    public class MultiDataSourceProperties {
    
        public static final String PREFIX = "spring.datasource.multi";
    
        /**
         * 必须设置默认的库,默认master
         */
        private String primary = "master";
    
        private Map<String, DataSourceProperty> datasource = new LinkedHashMap<>();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3、配合@Bean注解在配置类的Bean定义方法上使用

      @Configuration注解的配置类中通过@Bean注解在某个方法上将方法返回的对象定义为一个Bean,并使用配置文件中相应的属性初始化该Bean的属性。

    @Configuration
    public class DataSourceConfig {
    	@Primary
    	@Bean(name = "primaryDataSource")
    	@ConfigurationProperties(prefix="spring.datasource.primary")
    	public DataSource primaryDataSource() {
    		return DataSourceBuilder.create().build();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4、配合@EnableConfigurationProperties使用

      使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationProperties定义为Bean。

      下面示例和前面的代码类似,但是这里没有使用@Configuration注解,而是在MultiDataSourceAutoConfiguration配置类中使用@EnableConfigurationProperties注解进行配置。

    @Slf4j
    @Getter
    @Setter
    @ConfigurationProperties(prefix = MultiDataSourceProperties.PREFIX)
    public class MultiDataSourceProperties {
    
        public static final String PREFIX = "spring.datasource.multi";
        /**
         * 必须设置默认的库,默认master
         */
        private String primary = "master";
        private Map<String, DataSourceProperty> datasource = new LinkedHashMap<>();
    }
    
    
    @Configuration
    @EnableConfigurationProperties(MultiDataSourceProperties.class)
    public class MultiDataSourceAutoConfiguration {
    
        private final MultiDataSourceProperties properties;
    
        public MultiDataSourceAutoConfiguration(MultiDataSourceProperties properties) {
            this.properties = 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

    5、@ConfigurationProperties 注解 与 @Value注解

      @ConfigurationProperties注解 和 @Value注解 都可以实现读取配置文件值注入到JavaBean,但是在用法上有一些区别:

    1. 使用位置
      @ConfigurationProperties标注在JavaBean的类名上;而@Value标注在JavaBean的属性上。
    2. 注入参数功能
      @ConfigurationProperties 注解,可以批量注入配置文件中的属性;@Value注解,只能一个一个来指定。
    3. 松散绑定
      松散绑定,即:如果你的属性为 lastName 这种格式,你可以通过 last-name 的方式成功绑定,反之亦可成功绑定。(驼峰编写,会自动转成-小写字母的方式;-小写字母的方式也会识别成驼峰,本质就是两个能相互被识别。@ConfigurationProperties 注解,支持松散绑定;@Value注解,就不支持松散绑定了,必须是一模一样的名称,才可以正常绑定。
    4. EL表达式
      @ConfigurationProperties 注解,不支持EL表达式,此处不作介绍。 但是 @Value 是可以支持 EL 表达式。
    5. JSR303数据校验
      @ConfigurationProperties 注解,支持 JSR303 数据校验;@Value注解,不支持JSR303数据校验。
    6. 复杂类型封装
      @ConfigurationProperties 注解,支持复杂类型(对象等)的直接封装;@Value注解,不支持复杂类型封装。

    关于@ConfigurationProperties 注解 与 @Value注解更详细的对比可以参考《注解@ConfigurationProperties 和 @Value 对比》

    6、Not registered via @EnableConfigurationProperties or marked as Spring component 报错

      在使用@ConfigurationProperties 注解时,出现“Not registered via @EnableConfigurationProperties or marked as Spring component”报错信息时,说明没有正确使用该注解,主要是没有在使用@ConfigurationProperties 注解的类上配合使用@Component注解使用或者没有配合@EnableConfigurationProperties这个注解进行注册为属性配置的类。

  • 相关阅读:
    bindParam() 和 bindValue() 的区别
    指导与管理项目执行
    AgileBoot - 项目内统一的错误码设计
    TikTok如何为独立站引流?
    视觉SLAM十四讲_4李群与李代数
    深入理解 Web 协议:HTTP 2
    【Python函数式编程】——闭包
    Mysql 都有那些最需要掌握的原理?
    【核心动画-组动画-CAAnimationGroup Objective-C语言】
    18_ue4进阶末日生存游戏开发[创建运行时UI]
  • 原文地址:https://blog.csdn.net/hou_ge/article/details/128207212