重学SpringBoot3-EnableWebMvcConfiguration

本文详细介绍了SpringBoot3中的EnableWebMvcConfiguration、WebMvcConfigurationSupport类以及@EnableWebMvc注解的作用、区别和使用场景,包括自定义SpringMVC配置的方法。
摘要由CSDN通过智能技术生成

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》
期待您的点赞👍收藏⭐评论✍

上一篇文章对 SpringMVC 重要配置类—— WebMvcAutoConfiguration 类进行了介绍,下面介绍下它引入的另外几个重要组件,它们分别是EnableWebMvcConfiguration 类、WebMvcConfigurationSupport 类和@EnableWebMvc注解,下图是它们的关系:

它们的关系

1、EnableWebMvcConfiguration类

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.EnableWebMvcConfiguration

EnableWebMvcConfiguration 类是 Spring Framework 中的一个关键配置类,它类负责注册并配置 Spring MVC 的各种组件(管理着配置文件中 spring.web.xxx开头的配置项),例如:

  • 视图解析器(ViewResolvers):用于将视图名(如 “home”)解析为实际的视图(如一个 JSP 文件或一个 Thymeleaf 模板)。
  • 消息转换器(MessageConverters):用于请求和响应的读写,例如将 Java 对象转换为 JSON 或 XML,反之亦然。
  • 静态资源处理(ResourceHandlers):用于提供对静态资源如图片、JS、CSS 文件的处理。
  • 拦截器(Interceptors)、参数解析器(ArgumentResolvers)和返回值处理器(ReturnValueHandlers)等。
@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)
@ImportRuntimeHints(WebResourcesRuntimeHints.class)
public class WebMvcAutoConfiguration { 
    // 其他组件
    
	@Configuration(proxyBeanMethods = false)
    @EnableConfigurationProperties(WebProperties.class)
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {}
    
}

在日常开发中我们不经常直接与它交互,这个类扩展自 DelegatingWebMvcConfiguration,后者又扩展自 WebMvcConfigurationSupport

2、WebMvcConfigurationSupport类

org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport

WebMvcConfigurationSupport 是 Spring MVC 框架的核心配置支持类,提供了 Spring MVC 基础设施的背后配置。这个类通常在不使用 Spring Boot 自动配置时直接或间接被应用,特别是当开发者需要完全控制 Spring MVC 配置时,简单来说如果你想完全自己配置 MVC,就是用 WebMvcConfigurationSupport 来完成的。

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {}

3、@EnableWebMvc注解

org.springframework.web.servlet.config.annotation.EnableWebMvc

该注解代码如下:

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

当你在配置类上使用 @EnableWebMvc 注解时,实际上是自己导入 DelegatingWebMvcConfiguration,它会引入 WebMvcConfigurationSupport 组件。这与 Spring Boot 自动配置 MVC 的方式不同(其实和 SpringBoot 自动配置互斥)。在不使用 Spring Boot,仅使用 Spring MVC 时,若想手动完全控制 MVC 配置(覆盖默认配置),就需要添加 @EnableWebMvc

所以使用 @EnableWebMvc 时,SpringBoot 的 WebMvcAutoConfiguration 都会失效。

4、EnableWebMvcConfiguration 和 WebMvcConfigurer的区别

  • 应用方式EnableWebMvcConfiguration 通过使用 @EnableWebMvc 注解间接应用,而 WebMvcConfigurer 是通过直接实现接口并重写其方法来应用。
  • 控制级别:使用 @EnableWebMvc(从而应用 EnableWebMvcConfiguration)意味着你想要完全控制 MVC 配置(手动挡),覆盖 Spring Boot 默认的 MVC 自动配置。而实现 WebMvcConfigurer 允许在不失去 SpringBoot 自动配置的情况下定制和扩展 MVC 配置(手自一体)。
  • 使用场景EnableWebMvcConfiguration 主要用于非 Spring Boot 应用或者当需要完全控制 Spring MVC 配置时。WebMvcConfigurer 适用于需要定制 Spring Boot 自动配置的 Spring MVC 行为的情况。

5、自定义 Spring MVC 配置

即使使用了 @EnableWebMvc,你仍然可以通过实现 WebMvcConfigurer 接口来自定义 Spring MVC 的配置。这种方式允许你在保持默认配置的基础上进行添加或覆盖。

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
    // 自定义配置,如添加视图解析器、消息转换器等
}

请注意,EnableWebMvcConfiguration 通常是 Spring 内部使用的,开发者通过 @EnableWebMvc 注解和 WebMvcConfigurer 接口与 Spring MVC 的配置交互,而不是直接与 EnableWebMvcConfiguration 类交互。这是 Spring 如何设计它的组件和配置类,以帮助实现依赖注入和分离关注点。

  • 27
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Springboot CAS-Client 是一个基于Springboot框架集成CAS(Central Authentication Service)的客户端。 CAS是一种单点登录(Single Sign-On)协议,它允许用户在一次登录后就能够访问多个应用,而无需重新认证。 Springboot CAS-Client 的作用是充当CAS服务端和应用系统之间的中间件,它负责向CAS服务端发送认证请求,并根据认证结果来管理用户的登录状态。 为了集成CAS,我们首先需要在Springboot项目中引入相应的依赖,例如spring-boot-starter-webspring-boot-starter-security。接着,我们需要配置CAS服务端的地址信息,包括CAS服务端的登录URL、登出URL以及验证票据的URL等。 在Springboot CAS-Client中,我们也可以自定义一些过滤器和拦截器来实现相关的功能。例如,我们可以编写一个CAS认证过滤器来拦截所有的请求,并判断用户的登录状态。如果用户未登录,则跳转到CAS服务端进行认证;如果用户已登录,则直接放行请求。此外,我们还可以编写一个CAS登出拦截器来处理用户的登出请求,并在登出完成后将用户重定向到指定的页面。 总的来说,Springboot CAS-Client 提供了一个简洁、灵活的方式来集成CAS协议,使得我们的Springboot应用能够享受到单点登录带来的便利。通过它,我们可以轻松地实现用户认证、登录状态管理以及注销等功能,提升用户体验并提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoderJia_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值