当前位置:网站首页>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 .
边栏推荐
- [Yugong series] February 2022 U3D full stack class 008 - build a galaxy scene
- Compilation and linking of programs
- Through the "last mile" of legal services for the masses, fangzheng Puhua labor and personnel law self-service consulting service platform has been frequently "praised"
- Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
- 详解华为应用市场2022年逐步减少32位包体上架应用和策略
- 注解@ConfigurationProperties的三种使用场景
- Analysis of using jsonp cross domain vulnerability and XSS vulnerability in honeypot
- Pvtv2--pyramid vision transformer V2 learning notes
- What are the advantages of commas in conditional statements- What is the advantage of commas in a conditional statement?
- Laravel8 uses passport login and JWT (generate token)
猜你喜欢
Golang 编译约束/条件编译 ( // +build <tags> )
南京商品房买卖启用电子合同,君子签助力房屋交易在线网签备案
打通法律服务群众“最后一公里”,方正璞华劳动人事法律自助咨询服务平台频获“点赞”
Novice entry SCM must understand those things
The single value view in Splunk uses to replace numeric values with text
IP地址的类别
数据分片介绍
SSM 整合
Merge sort and non comparison sort
Low success rate of unit test report
随机推荐
GFS distributed file system
The single value view in Splunk uses to replace numeric values with text
Golan idea IntelliJ cannot input Chinese characters
Snyk dependency security vulnerability scanning tool
注解@ConfigurationProperties的三种使用场景
2-3查找樹
[untitled]
Laravel8 uses passport login and JWT (generate token)
Iptables' state module (FTP service exercise)
調用華為遊戲多媒體服務的創建引擎接口返回錯誤碼1002,錯誤信息:the params is error
opencv 将16位图像数据转为8位、8转16
Open3d ISS key points
Other 7 features of TCP [sliding window mechanism ▲]
Data type - integer (C language)
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
National SMS center number inquiry
Arm GIC (IV) GIC V3 register class analysis notes.
Merge sort and non comparison sort
You should use Google related products with caution