当前位置:网站首页>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 .
边栏推荐
- Laravel8 uses passport login and JWT (generate token)
- Leetcode 1984. Minimum difference in student scores
- Three series of BOM elements
- Opencv learning note 4 - expansion / corrosion / open operation / close operation
- What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
- 注解@ConfigurationProperties的三种使用场景
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
- Several ways of lambda used in functions in kotlin (higher-order functions)
- GOLand idea intellij 无法输入汉字
- A single game with goods increased by 100000, and the rural anchor sold men's clothes on top of the list?
猜你喜欢
Other 7 features of TCP [sliding window mechanism ▲]
Opencv learning note 4 - expansion / corrosion / open operation / close operation
idea里使用module项目的一个bug
Low success rate of unit test report
Rsync remote synchronization
Iptables' state module (FTP service exercise)
21 general principles of wiring in circuit board design_ Provided by Chengdu circuit board design
In go language, function is a type
[南京大学]-[软件分析]课程学习笔记(一)-introduction
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
随机推荐
POJ - 3616 Milking Time(DP+LIS)
Opencv learning note 5 - gradient calculation / edge detection
Obsidan之数学公式的输入
Golang compilation constraint / conditional compilation (/ / +build < tags>)
Three series of BOM elements
SSM 整合
【MySQL】数据库进阶之触发器内容详解
Train your dataset with swinunet
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
leetcode134. gas station
Go write a program that runs within a certain period of time
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
对API接口或H5接口做签名认证
Gson转换实体类为json时报declares multiple JSON fields named
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
为什么要选择云原生数据库
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Data type - floating point (C language)
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University