先说作用:

@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效

说明:

用springboot开发的过程中,我们会用到@ConfigurationProperties注解,主要是用来把properties或者yml配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。
如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的,当然在@ConfigurationProperties加入注解的类上加@Component也可以使交于springboot管理。

说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。

测试:
@ConfigurationProperties 与 @EnableConfigurationProperties 的关系。

@EnableConfigurationProperties 文档中解释:
@EnableConfigurationProperties注解应用到你的@Configuration时, 任何被@ConfigurationProperties注解的beans将自动被Environment属性配置。 这种风格的配置特别适合与SpringApplication的外部YAML配置进行配合使用。

测试发现:
1.使用 @EnableConfigurationProperties 进行注册

@ConfigurationProperties(prefix = "service.properties")
public class HelloServiceProperties {private static final String SERVICE_NAME = "test-service";private String msg = SERVICE_NAME;set/get
}@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {}@RestController
public class ConfigurationPropertiesController {@Autowiredprivate HelloServiceProperties helloServiceProperties;@RequestMapping("/getObjectProperties")public Object getObjectProperties () {System.out.println(helloServiceProperties.getMsg());return myConfigTest.getProperties();}
}

自动配置设置

service.properties.name=my-test-name
service.properties.ip=192.168.1.1
service.user=kayle
service.port=8080

一切正常,但是 HelloServiceAutoConfiguration 头部不使用 @EnableConfigurationProperties,测访问报错。

2.不使用 @EnableConfigurationProperties 进行注册,使用 @Component 注册

@ConfigurationProperties(prefix = "service.properties")
@Component
public class HelloServiceProperties {private static final String SERVICE_NAME = "test-service";private String msg = SERVICE_NAME;public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}}

Controller 不变,一切正常,如果注释掉 @Component 测启动报错。
由此证明,两种方式都是将被 @ConfigurationProperties 修饰的类,加载到 Spring Env 中。

测试环境:SpringBoot1.5

@EnableConfigurationProperties是怎么加载的

通过查看@EnableConfigurationProperties的注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EnableConfigurationPropertiesImportSelector.class)
public @interface EnableConfigurationProperties {/*** Convenient way to quickly register {@link ConfigurationProperties} annotated beans* with Spring. Standard Spring Beans will also be scanned regardless of this value.* @return {@link ConfigurationProperties} annotated beans to register*/Class<?>[] value() default {};}

通过分析自动配置可以知道,肯定是这个类EnableConfigurationPropertiesImportSelector起的作用;@Import的作用就是把这个ImportSelector的selectImprts()方法返回的类名的类加载到IOC容器中。

/*** 将ConfigurationPropertiesBeanRegistrar、ConfigurationPropertiesBindingPostProcessorRegistrar这两个类注册到容器中。*/
@Override
public String[] selectImports(AnnotationMetadata metadata) {MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(EnableConfigurationProperties.class.getName(), false);Object[] type = attributes == null ? null: (Object[]) attributes.getFirst("value");if (type == null || type.length == 0) {return new String[] {ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };}return new String[] { ConfigurationPropertiesBeanRegistrar.class.getName(),ConfigurationPropertiesBindingPostProcessorRegistrar.class.getName() };
}

selectImports方法返回了这两个类:ConfigurationPropertiesBeanRegistrar和ConfigurationPropertiesBindingPostProcessorRegistrar,是何时加载的,我们只需要看这个类ConfigurationPropertiesBeanRegistrar即可:

/*** {@link ImportBeanDefinitionRegistrar} for configuration properties support.*/
public static class ConfigurationPropertiesBeanRegistrarimplements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(EnableConfigurationProperties.class.getName(), false);List<Class<?>> types = collectClasses(attributes.get("value"));for (Class<?> type : types) {// 类的@ConfigurationProperties的prefix属性值,默认为空字符串String prefix = extractPrefix(type);// prefix和类名的拼接String name = (StringUtils.hasText(prefix) ? prefix + "-" + type.getName(): type.getName());if (!registry.containsBeanDefinition(name)) {// 注册方法:根据找到的类名name和type,将加入注解@ConfigurationProperties的类加入spring容器里面registerBeanDefinition(registry, type, name);}}}
}

另外还有这个类:ConfigurationPropertiesBindingPostProcessorRegistrar,刚刚没有分析,看了下源码,其实他做的事情就是通过 ConfigurationPropertiesBindingPostProcessor 将配置文件当中的属性值赋予到加了@ConfigurationProperties的注解的类的属性上。

/*** {@link ImportBeanDefinitionRegistrar} for binding externalized application properties* to {@link ConfigurationProperties} beans.** @author Dave Syer* @author Phillip Webb*/
public class ConfigurationPropertiesBindingPostProcessorRegistrarimplements ImportBeanDefinitionRegistrar {/*** The bean name of the {@link ConfigurationPropertiesBindingPostProcessor}.*/public static final String BINDER_BEAN_NAME = ConfigurationPropertiesBindingPostProcessor.class.getName();private static final String METADATA_BEAN_NAME = BINDER_BEAN_NAME + ".store";@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,BeanDefinitionRegistry registry) {if (!registry.containsBeanDefinition(BINDER_BEAN_NAME)) {BeanDefinitionBuilder meta = BeanDefinitionBuilder.genericBeanDefinition(ConfigurationBeanFactoryMetaData.class);BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(ConfigurationPropertiesBindingPostProcessor.class);bean.addPropertyReference("beanMetaDataStore", METADATA_BEAN_NAME);registry.registerBeanDefinition(BINDER_BEAN_NAME, bean.getBeanDefinition());registry.registerBeanDefinition(METADATA_BEAN_NAME, meta.getBeanDefinition());}}}

参考:

https://www.jianshu.com/p/7f54da1cb2eb

关于 @EnableConfigurationProperties 注解相关推荐

  1. Spring Boot 关于 @EnableConfigurationProperties 注解 —— 使用 @ConfigurationProperties 注解的类生效。

    先说作用: @EnableConfigurationProperties注解的作用是:使用 @ConfigurationProperties 注解的类生效. 说明: 如果一个配置类只配置@Config ...

  2. @EnableConfigurationProperties 注解和@ConfigurationProperties注解实现配置绑定

    ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@Config ...

  3. Spring : @EnableConfigurationProperties注解

    1.美图 2.概述 @EnableConfigurationProperties注解的作用是让使用@ConfigurationProperties注解的类生效.你可以通过在@EnableConfigu ...

  4. @EnableConfigurationProperties 注解

    关于YAML与Properties文件中的key 与 JavaBean 中属性的对应关系,观测源代码 ConfigurationPropertyName.elementEquals 方法得知,有以下几 ...

  5. @EnableConfigurationProperties注解

    目录 1.概述 2.测试 2.1 使用 @EnableConfigurationProperties 进行注册 2.2 使用 @Component 注册 3.项目中的使用场景 1.概述 @Enable ...

  6. 关于@EnableConfigurationProperties 注解

    先说作用: @EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效. 说明: 如果一个配置类只配置@Confi ...

  7. SpringBoot - @EnableConfigurationProperties注解使用详解

    @EnableConfigurationProperties注解的作用是什么? 将标注了@ConfigurationProperties注解的类注入到Spring容器中.该注解是用来开启对@Confi ...

  8. SpringBoot之@EnableConfigurationProperties分析

    我们在用SpringBoot进行项目开发的时候,基本上都使用过@ConfigurationProperties这个注解,我们在之前的文章中也说过ConfigurationPropertiesBindi ...

  9. SpringBoot @Value、 @ConfigurationProperties 与 @EnableConfigurationProperties 使用

    文章目录 @Value 用法 @Value("#{}") @Value("${}") ConfigurationProperties 用法 使用@Compone ...

最新文章

  1. 脚本其实很简单-windows配置核查程序(1)
  2. sql 字符串函数(一)
  3. MongoDB在Windows系统下的安装和启动
  4. JS设计模式(2)策略模式
  5. LCD显示实验----STM32f4--HAL
  6. c/c++读取txt文件中指定行的内容_和尧名大叔一起从0开始学Python编程-简单读写文件
  7. raid5需要几块硬盘_Raid5盘阵2块硬盘损坏【热备盘未激活】数据恢复概述
  8. mysql 多个值求和_SQL优化大神玩转MySQL函数系列(2)LEAST,SUM的应用
  9. 用turtle库画五角星
  10. php radio用法,JavaScript_JQuery radio(单选按钮)操作方法汇总,随着Jquery的作用越来越大,使 - phpStudy...
  11. #大数加减乘除#校赛D题solve
  12. 玉龙雪山还会存在多久
  13. 给idea换自定义背景图片的快捷键
  14. Python的GUI图形界面工具大全
  15. html调用js自动播放音乐,使用html js实现点击文本和播放音乐的功能
  16. MBA提前面试:商学院看重软实力
  17. 【OpenCV】生成透明的PNG图像
  18. 【数据治理-01】开篇:一起聊聊数据治理
  19. 如何自创一门计算机语言
  20. Nginx+Docker+Jekyll+阿里云ECS+备案搭建博客全记录

热门文章

  1. 客户说我已经有合作伙伴了 电话销售如何回应
  2. elementUI el-date-picker表单组件
  3. 论Android产品高效开发之路
  4. php一句话木马调用cmd命令,一句话木马(webshell)是如何执行命令的
  5. LaTeX排版小工具
  6. 正点原子STM32F103学习笔记(六)——时钟系统
  7. 面试时遇到『看门狗』脖子上挂着『时间轮』,我就问你怕不怕?
  8. nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 的解决办法
  9. 反恐精英代码_CS:GO和军团要塞2源代码泄漏,Valve:稳住
  10. 《东周列国志》第六十二回 诸侯同心围齐国 晋臣合计逐栾盈