当前位置:网站首页>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 .
边栏推荐
- Low success rate of unit test report
- AVL平衡二叉搜索树
- [南京大学]-[软件分析]课程学习笔记(一)-introduction
- Ebpf cilium practice (1) - team based network isolation
- Golang 编译约束/条件编译 ( // +build <tags> )
- 一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
- 调用华为游戏多媒体服务的创建引擎接口返回错误码1002,错误信息:the params is error
- FPGA knowledge accumulation [6]
- [kuangbin]专题十五 数位DP
- IP地址的类别
猜你喜欢
Data type - integer (C language)
How to integrate app linking services in harmonyos applications
Practice of combining rook CEPH and rainbow, a cloud native storage solution
Deit learning notes
Novice entry SCM must understand those things
A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)
归并排序和非比较排序
The single value view in Splunk uses to replace numeric values with text
一种适用于应用频繁测试下快速查看Pod的日志的方法(grep awk xargs kuberctl)
Exercise arrangement 2.10, 11
随机推荐
对API接口或H5接口做签名认证
详解华为应用市场2022年逐步减少32位包体上架应用和策略
Other 7 features of TCP [sliding window mechanism ▲]
AVL balanced binary search tree
Go语言中,函数是一种类型
Rapid integration of authentication services - harmonyos platform
Data type - integer (C language)
Opencv learning notes 1 -- several methods of reading images
Implementation of navigation bar at the bottom of applet
[Yugong series] February 2022 U3D full stack class 006 unity toolbar
Implementation method of data platform landing
Iptables' state module (FTP service exercise)
Coquette data completes the cloud native transformation through rainbow to realize offline continuous delivery to customers
Obsidan之数学公式的输入
Download and install orcale database11.2.0.4
About using CDN based on Kangle and EP panel
POJ - 3616 Milking Time(DP+LIS)
2 - 3 arbre de recherche
leetcode134. gas station
National SMS center number inquiry