当前位置:网站首页>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 .
边栏推荐
- Tronapi-波场接口-源码无加密-可二开--附接口文档-基于ThinkPHP5封装-作者详细指导-2022年7月6日-新手快速上手-可无缝升级tp6版本
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- leetcode135. Distribute candy
- Grpc, oauth2, OpenSSL, two-way authentication, one-way authentication and other column directories
- Implementation of navigation bar at the bottom of applet
- Rainbow version 5.6 was released, adding a variety of installation methods and optimizing the topology operation experience
- Open3d ISS key points
- POJ - 3616 Milking Time(DP+LIS)
- One click deployment of highly available emqx clusters in rainbow
- mysql分区讲解及操作语句
猜你喜欢

Opencv learning note 5 - gradient calculation / edge detection

Quick sorting (detailed illustration of single way, double way, three way)

Open3d ISS key points

What is the method of manual wiring in PCB design in 22protel DXP_ Chengdu electromechanical Development Undertaking

A method for quickly viewing pod logs under frequent tests (grep awk xargs kuberctl)

2 - 3 arbre de recherche

Componentspace2022, assertions, protocols, bindings, and configuration files
![Other 7 features of TCP [sliding window mechanism ▲]](/img/ff/c3f52a7b89804acfd0c4f3b78bc4a0.jpg)
Other 7 features of TCP [sliding window mechanism ▲]

SSM 整合

2-3查找树
随机推荐
[kuangbin]专题十五 数位DP
Train your dataset with swinunet
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
POJ - 3784 Running Median(对顶堆)
使用AGC重签名服务前后渠道号信息异常分析
SSM 整合
How to understand distributed architecture and micro service architecture
redis故障处理 “Can‘t save in background: fork: Cannot allocate memory“
Exercise arrangement 2.10, 11
Leetcode 1984. Minimum difference in student scores
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Input of mathematical formula of obsidan
Basic data types and string types are converted to each other
Pvtv2--pyramid vision transformer V2 learning notes
About using CDN based on Kangle and EP panel
Novice entry SCM must understand those things
为什么要选择云原生数据库
The single value view in Splunk uses to replace numeric values with text
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
【踩坑】nacos注册一直连接localhost:8848,no available server