@EnableConfigurationProperties 的作用
先说作用:
@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 中。
@EnableConfigurationProperties 的作用相关推荐
- SpringBoot复习:5(配置绑定)
配置绑定 在SpringBoot中我们如何读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用呢? 我们可以使用以下两种方式来实现. 方法一:@Configurati ...
- SpringBoot实战之SpringBoot自动配置原理
www.cnblogs.com/leihuazhe/p- SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConf ...
- Sprig boot自动配置
1.概述 Spring Boot是Spring旗下众多的子项目之一,其理念是约定优于配置,它通过实现了自动配置(大多数用户平时习惯设置的配置作为默认配置)的功能来为用户快速构建出标准化的应用.Spri ...
- springboot自动配置的原理_SpringBoot实战:详解SpringBoot自动配置原理
SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @Confi ...
- SpringBoot :SpringBoot自动配置原理
1.美图 2.概述 SpringBoot 自动配置主要通过 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties ...
- SpringBoot系列之(二):2.注解
@Conditional 通过条件控制是否注入Bean: @ConditionalOnBean当给定的bean存在时,则实例化当前Bean: @ConditionalOnMissingBean当给定的 ...
- 201902-Java面试总结
最近两周都在面试,很多技术点都知道,但是很少实战应用,面试的时候描述就自然模糊不清,所以现在就两周面试个人情况总结如下(其实还是个人java基础知识需要巩固理解学习,加油ing): java Obje ...
- SpringBoot底层注解
1.@Configuration 作用: 1.给Spring容器注册组件,相当于之前在spring的xml文件中注册组件 2.被@Configuration标记的类被标记为一个配置类,相当于一个xml ...
- springboot(四)——@EnableConfigurationProperties是如何起作用的你知道吗
前言 用springboot开发的过程中,我们会用到@ConfigurationProperties注解,主要是用来把properties或者yml配置文件转化为bean来使用的,而@EnableCo ...
最新文章
- Scrum中如何实现一个Sprint?
- HTML 5新增的元素
- Windows Server 2008 R2 之三十故障转移群集(二)
- 2017.7.10 Redis报错:DENIED Redis is running in protected mode
- BUAA_OO_第一次作业总结
- Eureka的自我保护模式
- 【狂神说Redis】2Redis入门 2-2Redis部署在Linux(Ubuntu)
- 开源Android自定义思维导图控件ThinkMap树状图TreeView(类似xMind那种效果)
- Python中的乘方计算
- 不想工作了怎么破?那就去这4个地方看一看
- DBeaver(其他可视化工具一样的逻辑)连接IoTDBDriver教程
- 网站都变成灰色了,怎么实现的?
- 软件项目“免坑”指南
- html视频怎么转换成图片,WPS演示如何将图片转换成视频?
- Qt 隐式共享之深拷贝、浅拷贝
- 以太坊柏林升级前的紧急刹车
- IP-Guard文档防泄密解决方案
- 【译】如何提高工作效率——HOWTO: Be more productive
- 商场计算机管理系统,计算机专业毕业论文商场管理系统—数据库设计及人事管理模块设计.doc...
- mastercam西门子840d后处理_对MasterCAM9.0中SINUMERIK840D钻孔循环后处理的修改.pdf