SpringBoot中的@ConfigurationProperties注解解析
作者:明天天明~
这篇文章主要介绍了SpringBoot中的@ConfigurationProperties注解解析,Spring源码中大量使用了ConfigurationProperties注解,通过与其他注解配合使用,能够实现Bean的按需配置,该注解可以放在类上,也可以放在方法上,需要的朋友可以参考下
SpringBoot中的@ConfigurationProperties注解
Spring源码中大量使用了ConfigurationProperties注解,通过与其他注解配合使用,能够实现Bean的按需配置。
该注解有一个prefix属性,通过指定的前缀,绑定配置文件中的配置,该注解可以放在类上,也可以放在方法上。
配置文件中
Car类中:
@Component //将配置文件application.properties中的属性进行绑定, // 第一种方式:此类放到容器中加上@Component注解 //第二种方式:在Myconfig中使用注解@EnableConfigurationProperties(Car.class) // @EnableConfigurationProperties(Car.class)功能: // 1. 开启Car配置绑定功能 // 2. 把这个Car这个组件自动注册到容器中,就不用写@Component @ConfigurationProperties(prefix = "mycat") @Data @AllArgsConstructor @NoArgsConstructor @ToString public class Car { private String name; public Integer price; }
@ConfigurationProperties(prefix = "mycat")与配置文件中属性进行绑定
两种组合注解
1. @EnableConfigurationProperties(Car.class) +@ConfigurationProperties(prefix = "mycat"配置MyConfig类上标注开启Car类绑定功能,把这个Car类自动注册到容器中
2. @Component+@ConfigurationProperties(prefix = "mycat)
这样就能将实体类放入容器中,并与配置文件进行绑定
相当于setName以及setPrice
到此这篇关于SpringBoot中的@ConfigurationProperties注解解析的文章就介绍到这了,更多相关@ConfigurationProperties注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
- SpringBoot 注解 @AutoConfiguration 在 2.7 版本中被新增的使用方法详解
- springboot使用@ConfigurationProperties实现自动绑定配置参数属性
- SpringBoot中的@ConfigurationProperties注解的使用
- SpringBoot中@ConfigurationProperties自动获取配置参数的流程步骤
- Springboot之@ConfigurationProperties注解解读
- 关于SpringBoot的@ConfigurationProperties注解和松散绑定、数据校验
- SpringBoot中@AutoConfiguration和@Configuration区别