当前位置:网站首页>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 .
边栏推荐
- 更改当前文件夹及文件夹下文件日期shell脚本
- Implementation method of data platform landing
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- Upload an e-office V9 arbitrary file [vulnerability recurrence practice]
- Required String parameter ‘XXX‘ is not present
- POJ - 3784 Running Median(对顶堆)
- [hard core science popularization] working principle of dynamic loop monitoring system
- SSM integration
- The field value in Splunk subquery fuzzy matching CSV is*
- [kuangbin]专题十五 数位DP
猜你喜欢

Ebpf cilium practice (2) - underlying network observability

数据分片介绍

MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)

Pvtv2--pyramid vision transformer V2 learning notes

归并排序和非比较排序

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

idea里使用module项目的一个bug
![[Yugong series] February 2022 U3D full stack class 005 unity engine view](/img/19/dc1b2f74b9ec5f5808de3049793862.jpg)
[Yugong series] February 2022 U3D full stack class 005 unity engine view

Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers

Splunk query CSV lookup table data dynamic query
随机推荐
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
基本数据类型和string类型互相转化
Installation and configuration of PLSQL
All about PDF crack, a complete solution to meet all your PDF needs
PLSQL的安装和配置
POJ - 3616 Milking Time(DP+LIS)
Download and install orcale database11.2.0.4
IP guard helps energy enterprises improve terminal anti disclosure measures to protect the security of confidential information
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
Opencv learning notes II - basic image operations
Several ways of lambda used in functions in kotlin (higher-order functions)
Implementation of navigation bar at the bottom of applet
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
Opencv learning note 3 - image smoothing / denoising
Train your dataset with swinunet
ES6_ Arrow function
idea里使用module项目的一个bug
[Yugong series] February 2022 U3D full stack class 005 unity engine view
【MySQL】数据库进阶之触发器内容详解