当前位置:网站首页>注解@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的功能,对属性进行匹配赋值。
边栏推荐
- 数据分析方法论与前人经验总结2【笔记干货】
- One click installation of highly available Nacos clusters in rainbow
- Data type - floating point (C language)
- Implementation method of data platform landing
- The single value view in Splunk uses to replace numeric values with text
- 2-3查找树
- IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
- Rapid integration of authentication services - harmonyos platform
- [untitled]
- 南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
猜你喜欢

下载和安装orcale database11.2.0.4

Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience

路由信息协议——RIP

What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
![[IELTS speaking] Anna's oral learning records part2](/img/c4/ad7ba2394ee7a52b67b643aa45a7ae.png)
[IELTS speaking] Anna's oral learning records part2

Implementation of navigation bar at the bottom of applet

Go语言中,函数是一种类型

調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error

Practice of implementing cloud native Devops based on rainbow library app

Tuowei information uses the cloud native landing practice of rainbow
随机推荐
uniapp 微信小程序监测网络
Composer change domestic image
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
IP地址的类别
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
[machine learning] watermelon book data set_ data sharing
Le système mes est un choix nécessaire pour la production de l'entreprise
Using nocalhost to develop microservice application on rainbow
Data type - floating point (C language)
PLSQL的安装和配置
The single value view in Splunk uses to replace numeric values with text
使用SwinUnet训练自己的数据集
Opencv learning note 5 - gradient calculation / edge detection
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
Obsidan之数学公式的输入
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
[kuangbin]专题十五 数位DP
Mock.js用法详解
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
2 - 3 arbre de recherche