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

如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入

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

@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 中

@ConfigurationProperties 与 @EnableConfigurationProperties相关推荐

  1. @Value,@ConfigurationProperties,@EnableConfigurationProperties,@PropertySource,@PropertySources

    @Value 注解可以用来将外部的值动态注入到 Bean 中,在 @Value 注解中,可以使用 ${} 或 #{}.${} 与 #{} 的区别如下: (1)@Value("${}" ...

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

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

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

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

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

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

  5. @EnableConfigurationProperties

    参考:https://www.jianshu.com/p/7f54da1cb2eb 使用 @ConfigurationProperties 注解的类生效. 如果一个配置类只配置@Configurati ...

  6. @EnableConfigurationProperties 注解

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

  7. @EnableConfigurationProperties 进行注册

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

  8. @EnableConfigurationProperties 的作用

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

  9. 关于 @EnableConfigurationProperties 注解

    先说作用: @EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效. 说明: 用springboot开发的过程 ...

最新文章

  1. Facebook加入AI芯片大战,挖走Google芯片产品开发负责人
  2. MySQL内部开发人员如何看待MySQL组复制?
  3. 1.3 Quick Start中 Step 3: Create a topic官网剖析(博主推荐)
  4. WM有约II(二):持续改进
  5. Preparation for International Women's Day
  6. python3多线程自定义threading子类
  7. Nodejs 英雄管理系统
  8. 现代化多媒体教室的计算机系统,多媒体教室系统建设方案
  9. RegularExpressions
  10. 联邦学习FATE社区2021年度生态峰会倒计时1天,请查收这份参会攻略
  11. 加油,我看好你 本题由擂主Wfox提供 -flag{bc57380e-9f8d-4b1e-8432-794b54b5625f}
  12. 端口输出报错**Error** test5.ASM(60) Constant too large
  13. 最大数[抽象排序之抽象规则]
  14. httpPost.setHeader,httpPost.addHeader 区别
  15. 孙悟空吃蟠桃c语言编程,孙悟空吃光蟠桃是爱国行为
  16. PHP简单MVC架构
  17. 上海计算机一级同等水平,上海计算机等级考试哪一级最难(全国计算机一级考试难吗)...
  18. Pygame 官方文档 - Tutorials - 逐行的黑猩猩教程(Line By Line Chimp)
  19. dede文章采集管理php,dedecms采集过滤
  20. cesium 入门开发系列地图鹰眼功能(附源码下载)

热门文章

  1. 向量复习(一):定义、求解、四则运算、点积和叉积
  2. 【萌新向】c语言求解八数字(华容道)问题
  3. Numpy应用100问
  4. 归一化数字角频率_数字信号处理中的各种频率
  5. 三、Kubernetes集群的命令行工具kubectl
  6. 关于pom.xml一直提示Could not transfer artifact(无法搬运)+无法解析maven依赖(更新)的问题,我在学习springboot的过程中遇到的一些问题(持续更新中)
  7. Apad Qzone项目总结(一)---发布!!!
  8. 窗前花相映 一抹杏腮红 | 腮红用户画像分析
  9. linux 山东大学 试卷_绝密·启用前丨《2019年山东大学研究生高考试题》
  10. 关于Unity程序化生成地图的研究(Wave Function Collapse)