先说作用:

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

说明:

如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @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 中。

关于@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 注解的类生效. 说明: 用springboot开发的过程 ...

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

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

  8. SpringBoot之@EnableConfigurationProperties分析

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

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

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

最新文章

  1. 不带头节点的链表有哪些缺点_23张图!万字详解「链表」,从小白到大佬!
  2. 比肩清华!中山大学前1%学科数,并列全国第二!
  3. 毕业设计 c语言编译器的设计开发-字节代码格式设计与实现 开题报告,C语言编译器设计与实现...
  4. oracle中schema指的是什么?
  5. .NET Core开发日志——RequestDelegate
  6. 从控制台输入一个数 num 与之前定义的数 n 进行比较
  7. vue复制html表格单列,vue表格含有一列多选框,如何获取被选中的行的数据?
  8. 友商惭愧不?2000+的手机红米直接干到千元
  9. SQL SERVER数据库优化相关资料
  10. 【教程】Edraw Max使用教程:创建一个基本流程图
  11. snmpwalk 命令_Snmpwalk命令行示例
  12. QMT量化交易网格交易 源码
  13. idea中webapp无法添加到mian文件夹中
  14. 【计算机操作系统】新兴操作系统
  15. 乍得“随军”记 ——写在结婚一周年
  16. java 毕业设计源码-基于Vue助农扶贫电商平台ssm电子商务网站
  17. Vue中实现点击当前行变色
  18. 关于Linux下上网速度慢的问题
  19. 大型社死现场,两程序员互为彼此面试官
  20. 无间还是有道 SaaS“年度贡献奖”引猜想

热门文章

  1. IT时代周刊:2009年IT十大争议人物
  2. tableviewCell复用 重叠问题
  3. codeforces(D2. Coffee and Coursework (Hard Version))二分答案
  4. HDU 3853 LOOPS(概率DP)
  5. 工业应用中基于三维模型的6D目标检测综述
  6. 格式化时间 将2021-09-05T09:08:03.000Z 转换成 YYYY-MM-DD HH:mm:ss 格式
  7. 安全生产双重预防体系建设数字化解决方案
  8. 上传文件- rz -be -y
  9. html中怎么制作选择头像,网页中的个人头像选择框(转)
  10. java学习第183天,javaWeb学习第42天,复习第19天;p240-245(08/16)-6h