当前位置:网站首页>Three usage scenarios of annotation @configurationproperties
Three usage scenarios of annotation @configurationproperties
2022-07-07 08:41:00 【Dream and sea depth @ none】
stay Spring Boot Notes in @ConfigurationProperties There are three usage scenarios , In general, we only use one of the scenes most . This article takes you to understand the use of three scenarios .
Scene one
Use @ConfigurationProperties and @Component Annotation to bean On the definition class , here @Component Refers to the same class instantiation Bean Annotations .
The basic examples are as follows :
// Define a class as a bean Annotations , such as @Component,@Service,@Controller,@Repository
// perhaps @Configuration
@Component
// Indicates that the prefix in the configuration file is user1 The value of the property initializes the bean Definition produces bean The same name property of the instance
// When used, this definition produces bean when , Its properties name Would be Tom
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// Omit getter/setter Method
}
Corresponding application.properties The contents of the configuration file are as follows :
user1.name=Tom
In this scenario , When Bean When instantiated ,@ConfigurationProperties The following properties of the corresponding prefix will be associated with Bean Object's properties match . If the conditions are met, the assignment will be made .
Scene two
Use @ConfigurationProperties and @Bean The annotation is in the configuration class Bean In the way of definition . Take data source configuration for example :
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
}
Here is the prefix “spring.datasource.primary” Properties of , Assign a value to DataSource Corresponding property value .
@Configuration The configuration class of the annotation is passed through @Bean Annotation defines the object returned by a method as a Bean, And use the corresponding properties in the configuration file to initialize the Bean Properties of .
Scene three
Use @ConfigurationProperties Annotation to common class , And then through @EnableConfigurationProperties Defined as Bean.
@ConfigurationProperties(prefix = "user1")
public class User {
private String name;
// Omit getter/setter Method
}
here User Object is not used @Component Related note .
And the User Class is used as follows :
@SpringBootApplication
@EnableConfigurationProperties({User.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
In the above code , adopt @EnableConfigurationProperties Yes User When instantiating , Will be used to @ConfigurationProperties The function of , Match and assign properties .
边栏推荐
- opencv 将16位图像数据转为8位、8转16
- redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- go写一个在一定时间内运行的程序
- 【MySQL】数据库进阶之触发器内容详解
- [kuangbin]专题十五 数位DP
- Componentspace2022, assertions, protocols, bindings, and configuration files
- leetcode134. gas station
- Ebpf cilium practice (2) - underlying network observability
- Basic data types and string types are converted to each other
猜你喜欢

Implementation of navigation bar at the bottom of applet

Implement your own dataset using bisenet

Train your dataset with swinunet

Virtual address space

23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP

Opencv learning notes 1 -- several methods of reading images

Pvtv2--pyramid vision transformer V2 learning notes

Splunk query CSV lookup table data dynamic query

【MySQL】数据库进阶之触发器内容详解

iptables 之 state模块(ftp服务练习)
随机推荐
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
Count sort (diagram)
Arm GIC (IV) GIC V3 register class analysis notes.
[Yugong series] February 2022 U3D full stack class 005 unity engine view
Tips for using jeditabletable
Snyk dependency security vulnerability scanning tool
数据分片介绍
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
Qt Charts使用(重写QChartView,实现一些自定义功能)
Obsidan之数学公式的输入
Golang compilation constraint / conditional compilation (/ / +build < tags>)
You should use Google related products with caution
MES系統,是企業生產的必要選擇
Novice entry SCM must understand those things
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
详解华为应用市场2022年逐步减少32位包体上架应用和策略
【MySQL】数据库进阶之触发器内容详解
Merge sort and non comparison sort
go写一个在一定时间内运行的程序