当前位置:网站首页>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


边栏推荐
- Altium designer creates multiple class networks to realize different lineweights of multiple networks
- ini解析學習文檔
- Spin official tutorial
- High performance distributed execution framework ray
- Inner member of class 5: inner class
- Résoudre le problème de décompiler la compilation normale
- 1.3 - 码制
- 力扣------替换空格
- 【微信小程序:单选、多选样式,背景色,圆角】
- Subnet division and subnet summary
猜你喜欢

Bat usage details 2

Connect to remote server

Detailed description of methods in the interface

Gazebo installation, uninstall and upgrade

1.4 - 定点数与浮点数

Installation and initialization of MariaDB database

关注这场直播,了解能源行业双碳目标实现路径

C language: exercise 3

Unclear about glide loading picture

01. regular expression overview
随机推荐
Deep learning --- the weight of the three good students' scores (3)
Use and principle of completionservice (source code analysis)
MySQL中的InnoDB引擎
基础刷题(一)
1.3 - Code System
ETL为什么经常变成ELT甚至LET?
880. decoded string at index
Use of observer mode and status mode in actual work
从底层结构开始学习FPGA----RAM IP核及关键参数介绍
uniapp 微信小程序返回上一页并刷新
C语言:练习题三
C language final experiment report (student achievement management system) source code
1.2(补充)
ES6 array
2020-10-06
Docker is equipped with the latest MySQL image
Huawei full-scale Daniel shared the 598 page full-color Manual of network protocols for the first time
IO stream (file class introduction)
Go pack and unpack
Improve simulation speed during ROS and Px4 joint simulation