当前位置:网站首页>注解@ConfigurationProperties的三种使用场景
注解@ConfigurationProperties的三种使用场景
2022-07-07 05:56:00 【梦及海深@无】
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景。本篇文章带大家了解一下三种场景的使用情况。
场景一
使用@ConfigurationProperties和@Component注解到bean定义类上,这里@Component代指同一类实例化Bean的注解。
基本使用实例如下:
// 将类定义为一个bean的注解,比如 @Component,@Service,@Controller,@Repository
// 或者 @Configuration
@Component
// 表示使用配置文件中前缀为user1的属性的值初始化该bean定义产生的的bean实例的同名属性
// 在使用时这个定义产生的bean时,其属性name会是Tom
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// 省略getter/setter方法
}
对应application.properties配置文件内容如下:
user1.name=Tom
在此种场景下,当Bean被实例化时,@ConfigurationProperties会将对应前缀的后面的属性与Bean对象的属性匹配。符合条件则进行赋值。
场景二
使用@ConfigurationProperties和@Bean注解在配置类的Bean定义方法上。以数据源配置为例:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
这里便是将前缀为“spring.datasource.primary”的属性,赋值给DataSource对应的属性值。
@Configuration注解的配置类中通过@Bean注解在某个方法上将方法返回的对象定义为一个Bean,并使用配置文件中相应的属性初始化该Bean的属性。
场景三
使用@ConfigurationProperties注解到普通类,然后再通过@EnableConfigurationProperties定义为Bean。
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// 省略getter/setter方法
}
这里User对象并没有使用@Component相关注解。
而该User类对应的使用形式如下:
@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
上述代码中,通过@EnableConfigurationProperties对User进行实例化时,便会使用到@ConfigurationProperties的功能,对属性进行匹配赋值。
边栏推荐
- GFS分布式文件系统
- Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
- MES system is a necessary choice for enterprise production
- Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
- ES6_ Arrow function
- Teach you how to select PCB board by hand (II)
- [Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
- Train your dataset with swinunet
- What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
- Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
猜你喜欢
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
【无标题】
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
Open3D ISS关键点
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Golang compilation constraint / conditional compilation (/ / +build < tags>)
[IELTS speaking] Anna's oral learning records part2
Rsync remote synchronization
2 - 3 arbre de recherche
Iptables' state module (FTP service exercise)
随机推荐
2-3查找树
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Splunk中single value视图使用将数值替换为文字
下载和安装orcale database11.2.0.4
MES system is a necessary choice for enterprise production
mysql分区讲解及操作语句
How to understand distributed architecture and micro service architecture
uniapp 微信小程序监测网络
rsync远程同步
Teach you how to select PCB board by hand (II)
2-3 lookup tree
Ebpf cilium practice (2) - underlying network observability
Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
How to integrate app linking services in harmonyos applications
Snyk dependency security vulnerability scanning tool
Low success rate of unit test report
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Pvtv2--pyramid vision transformer V2 learning notes
Deit learning notes
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP