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


边栏推荐
- 583. deleting two strings - Dynamic Planning
- Dynamic routing job
- First experience of Galaxy Kirin
- Force buckle ------ replace blank space
- ES6 array traversal and Es5 array traversal
- VIM view file code
- Combat simulation system data
- Centos8 install redis
- To: k210 realizes face recognition (code interpretation attached)
- Traitement d'images 7 - amélioration d'images
猜你喜欢
随机推荐
A complete performance test process
Centos8 install redis
Collections tool class (V)
Control method of UAV formation
【微信小程序:单选、多选样式,背景色,圆角】
ES6 arrow function
Analysis of startup process of gazebo multi computer simulation
Cocos studio3.1 installation package win
How does Altium designer hide some temporarily unnecessary classes, such as GND
JS prototype chain object function relationship
gazebo/set_ model_ State topic driving UAV model through posture
[untitled]
1.2 (supplementary)
To: k210 realizes face recognition (code interpretation attached)
Getting started with research
反編譯正常回編譯出現問題自己解决辦法
程序猿入门攻略(十一)——结构体
Rotate dimension tool rolabelimg
A small template (an abstract class, a complete process is written in a method, the uncertain part is written in the abstract method, and then the subclass inherits the abstract class, and the subclas
Collection and method of traversing collection elements (1)


![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)






