当前位置:网站首页>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 .
边栏推荐
- 【微信小程序:缓存操作】
- 下载和安装orcale database11.2.0.4
- Novice entry SCM must understand those things
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- [paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
- SSM integration
- Xcit learning notes
- The single value view in Splunk uses to replace numeric values with text
- 23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
猜你喜欢
关于基于kangle和EP面板使用CDN
快速集成认证服务-HarmonyOS平台
[untitled]
Xcit learning notes
Rsync remote synchronization
【踩坑】nacos注册一直连接localhost:8848,no available server
如何在HarmonyOS应用中集成App Linking服务
Interpolation lookup (two methods)
Calling the creation engine interface of Huawei game multimedia service returns error code 1002, error message: the params is error
AVL balanced binary search tree
随机推荐
如何在快应用中实现滑动操作组件
National SMS center number inquiry
[hard core science popularization] working principle of dynamic loop monitoring system
MES系统,是企业生产的必要选择
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
[paper reading] icml2020: can autonomous vehicles identify, recover from, and adapt to distribution shifts?
grpc、oauth2、openssl、双向认证、单向认证等专栏文章目录
uniapp 微信小程序监测网络
Tuowei information uses the cloud native landing practice of rainbow
2-3查找樹
Implement your own dataset using bisenet
opencv 将16位图像数据转为8位、8转16
How to understand distributed architecture and micro service architecture
JEditableTable的使用技巧
Download and install orcale database11.2.0.4
redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
Learn how to compile basic components of rainbow from the source code
MES system is a necessary choice for enterprise production
AVL balanced binary search tree
Basic data types and string types are converted to each other