当前位置:网站首页>注解@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的功能,对属性进行匹配赋值。
边栏推荐
- 数据库存储---表分区
- Virtual address space
- Snyk dependency security vulnerability scanning tool
- GOLand idea intellij 无法输入汉字
- 【微信小程序:缓存操作】
- [kuangbin] topic 15 digit DP
- [Chongqing Guangdong education] organic electronics (Bilingual) reference materials of Nanjing University of Posts and Telecommunications
- 为什么要选择云原生数据库
- Low success rate of unit test report
- IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
猜你喜欢
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Opencv learning notes 1 -- several methods of reading images
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Data type - integer (C language)
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
All about PDF crack, a complete solution to meet all your PDF needs
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
Virtual address space
Ebpf cilium practice (2) - underlying network observability
随机推荐
Learn how to compile basic components of rainbow from the source code
基本数据类型和string类型互相转化
Several ways of lambda used in functions in kotlin (higher-order functions)
Virtual address space
Low success rate of unit test report
Required String parameter ‘XXX‘ is not present
归并排序和非比较排序
Splunk中single value视图使用将数值替换为文字
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
【微信小程序:缓存操作】
The single value view in Splunk uses to replace numeric values with text
GFS分布式文件系统
A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
Ebpf cilium practice (1) - team based network isolation
Input and output of floating point data (C language)
Opencv learning note 4 - expansion / corrosion / open operation / close operation
Rsync remote synchronization
SSM integration
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
Snyk 依赖性安全漏洞扫描工具