当前位置:网站首页>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 · GitHubhttps://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
边栏推荐
- Decompilation normal decompilation problems. Solve them yourself
- 原理:WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
- Px4 control mode summary
- HCIA day 1
- ES6 arrow function
- To: k210 realizes face recognition (code interpretation attached)
- Several C language implementations
- File Transfer Protocol,FTP文件共享服务器
- 1.7 - CPU performance indicators
- 01. regular expression overview
猜你喜欢
Multithreading advanced level
圖像處理7-圖像增强
1.9 - Cache
1.5 - 逻辑运算
Usage of case, casez and casex statements in Verilog
C language final experiment report (student achievement management system) source code
Detailed description of methods in the interface
Base64 explanation: playing with pictures Base64 encoding
1.8 - multi level storage
Use of observer mode and status mode in actual work
随机推荐
How does Altium designer hide some temporarily unnecessary classes, such as GND
KEIL - 下载调试出现“TRACE HW not present”
2020-10-06
深度学习---三好学生各成绩所占权重问题(3)
程序猿入门攻略(十一)——结构体
ini解析學習文檔
Unclear about glide loading picture
How to use unmarshaljson
Common NPM install errors
华泰炒股安全吗?我想网上开户。
Pycharm shortcut key
写一个C程序判断系统是大端字节序还是小端字节序
1.2 (supplementary)
Why does the verification code not refresh when clicked
Collection and method of traversing collection elements (1)
1.5 - 逻辑运算
File transfer protocol, FTP file sharing server
Dynamic routing job
A complete performance test process
原来你是这样的数组,终于学会了