当前位置:网站首页>Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide
Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide
2022-06-30 06:38:00 【OkidoGreen】
Source code address ( Contains all and springmvc dependent , Static file path settings ,request Request entry acceptance , Return value processing converter Settings, etc ):
spring-framework/WebMvcConfigurationSupport.java at b595dc1dfad9db534ca7b9e8f46bb9926b88ab5a · spring-projects/spring-framework · GitHub
https://github.com/spring-projects/spring-framework/blob/b595dc1dfad9db534ca7b9e8f46bb9926b88ab5a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.javaspring-framework/DelegatingWebMvcConfiguration.java at b595dc1dfad9db534ca7b9e8f46bb9926b88ab5a · spring-projects/spring-framework · GitHub
https://github.com/spring-projects/spring-framework/blob/b595dc1dfad9db534ca7b9e8f46bb9926b88ab5a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.java
(1) In this way, we just create a class inheritance WebMvcConfigurer
We know , stay Spring Boot 2.0 Then use your own configuration class to inherit WebMvcConfigurerAdapter when ,idea Will prompt that this class is out of date .
Generally, we will adopt the following two alternatives :
- Realization WebMvcConfigurer
- Inherit WebMvcConfigurationSupport
But inheritance WebMvcConfigurationSupport It will cause some problems
before this , Let's take a look first WebMvc Automatic configuration class WebMvcAutoConfiguration The definition of :

Notice the red box around this key statement :
1 |
|
See this line can understand , original SpringBoot Made this restriction , Only when WebMvcConfigurationSupport Class does not exist before it takes effect WebMvc Automatic configuration ,WebMvc The autoconfiguration class not only defines classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ Equal path mapping , Configuration files are also defined spring.mvc Configuration information at the beginning .
When WebMvcAutoConfiguration Failure to take effect will lead to the following problems :
1.WebMvcProperties and ResourceProperties invalid
Because the properties in both configuration classes are WebMvcAutoConfiguration Use in
When WebMvc Auto configuration failure (WebMvcAutoConfiguration Automatic configuration ) when , This will cause the view parser to fail to parse and return to the corresponding view
Solution :
1. stay SpringBoot1.X In the version of the , We can inherit from WebMvcConfigurerAdapter, Override the method you want to implement .
2. But in SpringBoot2.X The definition of ,WebMvcConfigurerAdapter Has been defined as @Deprecated, Let's take a look at the source code :

SpringBoot In fact, I have already told you WebMvcConfigurerAdapter since Spring5.0 It is not recommended to use this version , But you can achieve WebMvcConfigurer And rewrite relevant methods to achieve similar functions .
2. On the classpath HttpMessageConverter invalid
Such as :StringHttpMessageConverterConfiguration、MappingJackson2HttpMessageConverter , because HttpMessageConverters Holds all HttpMessageConverter Example , stay WebMvcAutoConfigurationAdapter It will inject HttpMessageConverters , So when WebMvcAutoConfigurationAdapter When not loaded , It will fail , Indirectly cause spring.http.encoding.charset And spring.jackson.date-format A false failure .
Such as :StringHttpMessageConverter Will use spring.http.encoding.charset To configure , The default encoding is :ISO-8859-1
1 2 3 4 5 6 7 8 |
|
Solution :
Because the known configuration classes have passed @Bean Register in the container , So we can use WebMvcConfigurationSupport when , adopt @Autowired Inject into the configuration class , And replace it WebMvcConfigurationSupport The default configuration is OK , Such as :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
It can also be re implemented according to requirements .
Add _
WebMvcConfigurer Class code analysis :
from EnableWebMvcConfiguration Configuration class starts , When it injects, the parent class injects Spring Everything in the container WebMvcConfigurer class

Then inject RequestMappingHandlerAdapter Bean It will call the parent class requestMappingHandlerAdapter Method



Get the configuration at this time , For example, the return result is customized Handler, WebMvcConfigurerComposite Will traverse the call WebMvcConfigurer Implementation class
addReturnValueHandlers Method , The custom configuration will be loaded into the default configuration


边栏推荐
- 程序猿入门攻略(十一)——结构体
- C语言:练习题三
- Using C language pure for loop to implement ilovey
- Improve simulation speed during ROS and Px4 joint simulation
- 1.9 - Cache
- 2020-10-06
- Data read / write: realize data read / write function based on C # script in unity
- Switch must be better than if Else fast
- Basic questions (I)
- Record a problem tracking of excessive load
猜你喜欢
随机推荐
关于Glide加载图片模糊不清楚
Static routing job
Record a problem tracking of excessive load
Rotate dimension tool rolabelimg
【我的创作纪念日】一周年随笔
Analysis of startup process of gazebo multi computer simulation
The 40g high-efficiency cloud disk purchased by Alibaba cloud is only 20g attached
ini解析学习文档
程序猿入门攻略(十一)——结构体
IO streams (common streams)
深度学习---三好学生各成绩所占权重问题(3)
HuaWei满级大牛首次分享出这份598页网络协议全彩手册
力扣------替换空格
Docker is equipped with the latest MySQL image
Control method of UAV formation
1.9 - 存储器的分类
1.6 - CPU组成
Collection and method of traversing collection elements (1)
【微信小程序:单选、多选样式,背景色,圆角】
ES6 array traversal and Es5 array traversal









