当前位置:网站首页>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
边栏推荐
- Using LNMP to build WordPress sites
- Matlab smooth curve connection scatter diagram
- Arduino 测量交流电流
- 一文搞定垃圾回收器
- Composition of interface
- Roman numeral to integer
- Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
- openresty ngx_lua正则表达式
- Registration and skills of hoisting machinery command examination in 2022
- LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
猜你喜欢

Hcip day 12 (BGP black hole, anti ring, configuration)

利用LNMP实现wordpress站点搭建

Spectrum analysis of ADC sampling sequence based on stm32

终于搞懂什么是动态规划的

基于脉冲神经网络的物体检测

2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank

Hcip day 11 (BGP agreement)

Ieventsystemhandler event interface

First, redis summarizes the installation types

Selenium+Pytest自动化测试框架实战
随机推荐
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
audiopolicy
Three.js-01 入门
Use of shell:for loop
Leetcode buys and sells stocks
Detailed explanation of pointer and array written test of C language
Media query: importing resources
3D reconstruction of point cloud
查看网页最后修改时间方法以及原理简介
3 find the greatest common divisor and the least common multiple
The method and principle of viewing the last modification time of the web page
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
CJ mccullem autograph: to dear Portland
C Primer Plus Chapter 9 question 9 POW function
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
Déterminer si un arbre binaire est un arbre binaire complet
Nacos installation and service registration
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
TOPSIS code part of good and bad solution distance method