当前位置:网站首页>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 .
边栏推荐
- 2 - 3 arbre de recherche
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
- AVL balanced binary search tree
- Opencv learning note 5 - gradient calculation / edge detection
- Download and install orcale database11.2.0.4
- Data type - floating point (C language)
- What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
- Go语言中,函数是一种类型
- Deit learning notes
- 说一个软件创业项目,有谁愿意投资的吗?
猜你喜欢

如何在快应用中实现滑动操作组件
![[untitled]](/img/b5/348b1d8b5d34cf10e715522b9871f2.png)
[untitled]

详解华为应用市场2022年逐步减少32位包体上架应用和策略

Using nocalhost to develop microservice application on rainbow

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

归并排序和非比较排序

Rainbow combines neuvector to practice container safety management

Low success rate of unit test report

Ebpf cilium practice (1) - team based network isolation

National standard gb28181 protocol video platform easygbs adds streaming timeout configuration
随机推荐
How to understand distributed architecture and micro service architecture
[untitled]
idea里使用module项目的一个bug
Le système mes est un choix nécessaire pour la production de l'entreprise
Thirteen forms of lambda in kotlin
Data type - floating point (C language)
测试踩坑 - 当已有接口(或数据库表中)新增字段时,都需要注意哪些测试点?
MES system is a necessary choice for enterprise production
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
【踩坑】nacos注册一直连接localhost:8848,no available server
leetcode135. Distribute candy
为什么要选择云原生数据库
[Yu Yue education] higher vocational English reference materials of Nanjing Polytechnic University
JS的操作
【MySQL】数据库进阶之触发器内容详解
How to integrate app linking services in harmonyos applications
let const
AVL平衡二叉搜索树
What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking
[Yu Yue education] C language programming reference of Zhongbei College of Nanjing Normal University