目录

1.概述

2.测试

2.1 使用 @EnableConfigurationProperties 进行注册

2.2 使用 @Component 注册

3.项目中的使用场景


1.概述

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

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

简单点说@EnableConfigurationProperties的功能类似于@Component。

2.测试

2.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();}
}

配置文件application.properties

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

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

2.2 使用 @Component 注册

不使用 @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 中。

3.项目中的使用场景

如下,在配置类NacosConfigAutoConfiguration的头上加注解@EnableConfigurationProperties(NacosConfigProperties.class),

而在NacosConfigProperties配置类本身并没有实现了@Component相关的注解,也就是说运行项目时,不会直接把NacosConfigProperties配置类注入到Spring 容器中,而是在执行NacosConfigAutoConfiguration这个配置类时才会去把NacosConfigProperties类注入到spring

如下,NacosConfigProperties类本身并没有@Component相关注解:

参考链接:关与 @EnableConfigurationProperties 注解 - 简书

@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 注解

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

  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. java游戏脱逃_‎App Store 上的“冒险的逃离筏船”
  2. iOS网络开发(8)文件下载的实现
  3. div中插入图片_Web前端开发基础知识,设置网页背景图,如何在网页中插入图片...
  4. rabbitmq-plugins.bat enable rabbitmq_management
  5. 桥接模式 - 设计模式学习
  6. 对软件研发项目管理的深入探讨
  7. 运维工程师之-MySQL的故障问题总结
  8. linux 生成2g文件吗,linux 32位系统 c++写大于2G文件
  9. 启动Activity不显示界面
  10. 怎么样更好的做好站内的优化
  11. 【空间分析】0 基本空间分析工具
  12. python切面_Python装饰器与面向切面编程
  13. STM32的ADC多通道采集的实现
  14. 计算机逻辑与 或 非的表达式,逻辑表达式
  15. RK3399与MIPI DSI之间在DRM架构下的联结关系
  16. angular 脏值检测基础流程
  17. Day07 Scanner 类、 Random 类、 ArrayList 类 6.20
  18. 稿定设计怎么去除水印?
  19. Yii中CGridView单元格组件和数据提供者的使用
  20. 由圆上三点确定圆心和半径(附PythonMatlab程序)

热门文章

  1. 微信读书项目记录(1)
  2. 猜数字小游戏(网页版)
  3. 滤波器设计中的频率归一化问题
  4. Maven-settings配置
  5. 计算机二级c编程题特殊技巧,计算机国家二级C机试编程题技巧
  6. Go语言自学系列 | golang开发工具
  7. 双目立体图像矫正方法简述
  8. 电子商务系统需求分析
  9. 解决时间差太大导致Windows无法同步时间
  10. 计算机科学导论论文文章,计算机科学导论论文(2)