当前位置:网站首页>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
边栏推荐
- Registration and skills of hoisting machinery command examination in 2022
- Boring boring
- TOPSIS code part of good and bad solution distance method
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- Hj16 shopping list
- The difference between MVVM and MVC
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- [secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
- Vision Transformer (ViT)
- Use of grpc interceptor
猜你喜欢
基于STM32的ADC采样序列频谱分析
February 13, 2022-4-symmetric binary tree
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
The method and principle of viewing the last modification time of the web page
Debian 10 installation configuration
Codeforces Global Round 19
The difference between MVVM and MVC
Ieventsystemhandler event interface
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
随机推荐
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Activate function and its gradient
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
派对的最大快乐值
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
2.13 summary
Finally understand what dynamic planning is
Three.JS VR看房
What is the process of building a website
基于脉冲神经网络的物体检测
Week 17 homework
Thoroughly understand JVM class loading subsystem
Selenium+Pytest自动化测试框架实战
One article deals with the microstructure and instructions of class
Registration and skills of hoisting machinery command examination in 2022
The difference between MVVM and MVC
[untitled]
Function default parameters, function placeholder parameters, function overloading and precautions
leecode-学习笔记
Three. Js-01 getting started