当前位置:网站首页>YML configuration, binding and injection, verification, unit of bean
YML configuration, binding and injection, verification, unit of bean
2022-07-05 23:09:00 【ZJH'blog】
Configured rules
- Try to be all lowercase
- Support for loose binding ( Lower case numbers underline )
- @Value Loose binding is not supported

Three ways to inject
The same Bean There can only be one injection , Multiple injections will report errors (Bean Is a singleton )
@Component Inject
1,config class
@Component// Loading configuration properties must be subject to spring Control and control , Inject first bean
@Data// Loading configuration properties must have set
@ConfigurationProperties(prefix = "aliyossphoto")
public class OSSphotoConfig {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2, Start class
Can directly run.getBean(OSSphotoConfig.class)
@SpringBootApplication
public class BootAjaxtestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);
OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
System.out.println(bean);
}
@Bean + @ConfigurationProperties
1,config Class cannot be injected @Component
@Data// Loading configuration properties must have set
public class OSSphotoConfig {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2, The startup class needs @Bean Inject , as for @ConfigurationProperties(prefix = “”)
You can write anywhere , As long as config If there is a binding in the class or startup class
@SpringBootApplication
public class BootAjaxtestApplication {
@Bean
@ConfigurationProperties(prefix = "aliyossphoto")
public OSSphotoConfig getbean(){
OSSphotoConfig osSphotoConfig = new OSSphotoConfig();
return osSphotoConfig;
}
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);
OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
System.out.println(bean);
}
@EnableConfigurationProperties({class,class})
- This annotation can clearly see what has been injected Bean, At the same time @Component Conflict , Also with the @Bean Conflict ; however Need to be in config Class
- {} Using reflection , Multiple can be injected at the same time Bean
1,config class
@ConfigurationProperties(prefix = "aliyossphoto")
@Data// Loading configuration properties must have set
public class OSSphotoConfig {
private String endpoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2, Start the class
@EnableConfigurationProperties({OSSphotoConfig.class})
@SpringBootApplication
public class BootAjaxtestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);
OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
System.out.println(bean);
}}
Bean The operation of
- run.getBean(xxx.class) The object obtained is due to @Data The existence of can call get set Method
- Get the attribute value in addition to get The method can also be used @Value("${xx.xx}")
- In main class , Test class ,MVC Layers can be directly @Autowired obtain , But in the ordinary class, you need to use the tool class
Tool class implementation ApplicationContextAware Interface
Create a tool class under any package , This tool class can be obtained through reflection bean, No need to pass @Autowired or @Bean It can be used in ordinary classes bean
And this tool class is general
Use it directly SpringContext.getBean(xxx.class) obtain bean that will do
@Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { applicationContext = context; } // Static loading applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } // Get by reflection Bean public static <T> T getBean(Class<T> requiredType){ return getApplicationContext().getBean(requiredType); } // adopt id Name acquisition bean public static <T> T getBean(String name){ return (T) getApplicationContext().getBean(name); } }
@Value
Unit configuration
@XxxxxUnit
Property verification validation+validator
1,maven coordinate , You can't have one without the other
1,1 JSR303 standard
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
1,2 Hibernate validator Property validator
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
2, Enable attribute injection verification @[email protected] etc.
@ConfigurationProperties(prefix = "aliyossphoto")
@Data// Loading configuration properties must have set
@Validated
public class OSSphotoConfig {
@Max(value = 200, message = " The maximum value does not exceed 200")
@Min(value = 0 , message = " The minimum value cannot be lower than 0")
private Integer age;
}
There are many verification rules , Just search it when you have specific needs
边栏推荐
- 视频标准二三事
- One article deals with the microstructure and instructions of class
- Leecode learning notes
- Element positioning of Web Automation
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- Element operation and element waiting in Web Automation
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
- [untitled]
- Selenium+Pytest自动化测试框架实战
- [screen recording] how to record in the OBS area
猜你喜欢

Expectation, variance and covariance

Three.js-01 入门

Basic knowledge of database (interview)

Using LNMP to build WordPress sites

Alibaba Tianchi SQL training camp task4 learning notes

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~

查看网页最后修改时间方法以及原理简介

关于MySQL的30条优化技巧,超实用

Vcomp110.dll download -vcomp110 What if DLL is lost

Marginal probability and conditional probability
随机推荐
Basic knowledge of database (interview)
fibonacci search
Masked Autoencoders Are Scalable Vision Learners (MAE)
openresty ngx_lua正則錶達式
数据库基础知识(面试)
判斷二叉樹是否為完全二叉樹
视频标准二三事
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
Matlab smooth curve connection scatter diagram
第十七周作业
Alibaba Tianchi SQL training camp task4 learning notes
Three.js-01 入门
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
C Primer Plus Chapter 9 question 10 binary conversion
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
H5c3 advanced - player
基于脉冲神经网络的物体检测
Composition of interface