先说作用:

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

说明:

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

测试证明:
@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 中。

作者:咪雅先森
链接:https://www.jianshu.com/p/7f54da1cb2eb
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

关与 @EnableConfigurationProperties 注解相关推荐

  1. @EnableConfigurationProperties注解

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

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

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

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

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

  4. Spring : @EnableConfigurationProperties注解

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

  5. @EnableConfigurationProperties 注解

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

  6. 关于 @EnableConfigurationProperties 注解

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

  7. 关于@EnableConfigurationProperties 注解

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

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

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

  9. xxljob问题排查

    问题描述 xxljob自动注册失败, Ip地址为空, 查看日志发现XxlJobConfig中@Bean未加载 SpringBoot版本: 2.0.2.RELEASE 解决方案 使用以下两种配置方法均可 ...

  10. SpringBoot之@EnableConfigurationProperties分析

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

最新文章

  1. c++ 重载 重写_Java | 深入理解方法调用的本质(含重载与重写区别)
  2. 实验十 配置EIGRP协议
  3. 解决重写父类的方法且不会影响继承的子类的问题
  4. 实战|渗透学校某内网服务器
  5. SecureCRT连接CentOS阿里云,小键盘在VIM情况下,无法输入数字反而出现英文
  6. 将终结点图添加到你的ASP.NET Core应用程序中
  7. java this() super()_java中的this和super
  8. Redis:数据并发竞争顺序性
  9. 8080端口被占用怎么解决_端口占用不会搞?两行命令就解决!
  10. GDALSetProjection使用的一个注意事项
  11. Linux下让Eclipse支持Python
  12. 2014-07-22 如何成为一名合格的职业人士
  13. PHP中的正则表达式函数
  14. Semantic Role Labeling (SRL)
  15. 概率论与数理统计浙大第五版 第四章 部分习题
  16. 4个小时实现一个HTML5音乐播放器
  17. 什么ftp扫描工具好用,你所认识的什么ftp扫描工具好用
  18. matlab混沌映射,混沌映射MATLAB仿真.docx
  19. Cubieboard CC-A80性能之Ubuntu Linaro编译安装Node.js
  20. 洛谷 3373 线段树

热门文章

  1. reconnecting-websocket.min.js
  2. 《进化——我们在互联网上奋斗的故事》一一1.9 职业素养中的品德细节
  3. Survivor空间溢出实例
  4. CentOS+Asterisk+Freepbx
  5. Windows 8 各个版本区别对比
  6. 【黑金视频连载】NIOS II视频教程正式连载了!!!!!!!!
  7. 考前突击!等级考试高分攻略!(整理版)
  8. DataGrid固定列宽
  9. 把自定义的demuxer加入ffmpeg源码
  10. Linux SWAP 深度解读