当前位置: 首页 > news >正文

SpringMVC的自定义配置和自动化配置

SpringBoot的自动配置MVC处理加载逻辑

基于Spring Boot的MVC自动化配置由WebMvcAutoConfiguration类完成,部分关键源码:

@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
public class WebMvcAutoConfiguration {//省略源码......@SuppressWarnings("deprecation")@Configuration(proxyBeanMethods = false)@Import(EnableWebMvcConfiguration.class)@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })@Order(0)public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {//省略源码......}@Configuration(proxyBeanMethods = false)@EnableConfigurationProperties(WebProperties.class)public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {//省略源码......}
}
  • 自动化配置类(带有注解@AutoConfiguration)可以通过/META-INF/spring.factories文件引入

  • 从WebMvcAutoConfiguration的类级注解@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })可以看出类路径下的Servlet、DispatcherServlet和WebMvcConfigurer类会触发SpringBoot的自动化配置

  • 从WebMvcAutoConfiguration的类级注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)可以看出,自定义配置的WebMvcConfigurationSupport类会使得SpringBoot的MVC自动化配置失效,那么自定义配置的WebMvcConfigurationSupport将全面接管MVC的配置

  • WebMvcAutoConfigurationAdapter(WebMvcAutoConfiguration的静态内部类)会引入EnableWebMvcConfiguration配置类(WebMvcAutoConfiguration的静态内部类),EnableWebMvcConfiguration是DelegatingWebMvcConfiguration的一个子类,在DelegatingWebMvcConfiguration类中会自动注入所有的MVC配置类WebMvcConfigurer,DelegatingWebMvcConfiguration的部分源码如下:

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {//省略源码......@Autowired(required = false)public void setConfigurers(List<WebMvcConfigurer> configurers) {if (!CollectionUtils.isEmpty(configurers)) {this.configurers.addWebMvcConfigurers(configurers);}}//省略源码......}

使用@EnableWebMvc配置MVC

从注解源码可以看出@EnableWebMvc引入一个DelegatingWebMvcConfiguration类实现MVC的配置,DelegatingWebMvcConfiguration的引入会使得SpringBoot的MVC自动化配置全部失效(因为DelegatingWebMvcConfiguration是WebMvcConfigurationSupport的子类,前文已经讲述过原因),所有的MVC相关的配置都需要手动完成,所以SpringBoot项目不建议使用@EnableWebMvc处理SpringMVC的配置。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {}

那么怎样既可以保留SpringBoot关于MVC的自动化配置又能自定义扩展配置呢,不要使用不使用@EnableWebMvc注解注解,同时有以下两种方式扩展配置:

  1. 直接实现WebMvcConfigurer接口实现MVC自定义扩展配置(建议)

  1. 直接继承WebMvcConfigurerAdapter实现MVC自定义扩展配置

这里需要注意的是实现WebMvcConfigurer接口和继承WebMvcConfigurerAdapter是一样的效果,其实WebMvcConfigurerAdapter就是WebMvcConfigurer接口的一个空实现,因为Java8之后接口中可以存在default方法,WebMvcConfigurerAdapter中的空方法可以直接定义为WebMvcConfigurer中的default方法

http://www.lryc.cn/news/30449.html

相关文章:

  • 画图说透 ZooKeeper如何保证数据一致性:选举和ZAB协议
  • 错误异常捕获
  • js垃圾回收机制
  • YApi分析从NoSQL注入到RCE远程命令执行.md
  • 【C++】stl_list介绍和实现,list和vector区别,list vector string 迭代器失效
  • linux-kernel-ecmp-ipv4
  • 蒙特卡洛树搜索(MTCS)
  • 【Verilog】——Verilog简介
  • 【Python从入门到进阶】10、流程控制语句-循环语句(for-while)
  • 超全的命令(代码)执行漏洞无回显的姿势总结(附带详细代码和测试分析过程)
  • STM32MP157-Linux音频应用编程-简易语音助手
  • Python-OpenCV图像处理:学习图像算术运算,如加减法、图像混合、按位运算,以及如何实现它们
  • 并发编程——ReentrantLock
  • English Learning - L2 第 3 次小组纠音 [ʌ] [ɒ] [ʊ] [ɪ] [ə] [e] 2023.3.4 周六
  • STM32之关门狗
  • Apollo控制部分1-- ControlComponent组件介绍
  • 0626-0631韩顺平Java Buffered字节处理流 学习笔记
  • 【网络】序列化和反序列化
  • 【代码随想录训练营】【Day32】第八章|贪心算法|122.买卖股票的最佳时机II |55. 跳跃游戏|45.跳跃游戏II
  • constexpr 和 常量表达式
  • Vue响应式原理————Object.defineProperty()和proxy的用法分享
  • CSDN 编程竞赛三十四期题解
  • C#教程06 运算符
  • 软测入门(六)pytest单元测试
  • 经典分类模型回顾5—DenseNet实现图像分类(matlab)
  • 基于flask+bootstrap+echarts+mysql的鱼村小馆订餐后台管理系统
  • Spark使用Log4j将日志发送到Kafka
  • c++类与对象整理(上)
  • Docker学习(十九)什么是镜像的元数据?
  • Python如何获取弹幕?给你介绍两种方式