Spring Web MVC配置方式汇总(五)

​1 Spring Web MVC

本文将介绍Spring Web MVC的几种配置方法。

2 @EnableWebMvc

在上一篇文章中,我们已经使用过@EnableWebMvc注解进行Spring Web MVC的配置。@EnableWebMvc注解启用Spring Web MVC自动配置的特性,用于初始化Spring Web MVC框架。那么,自动配置到底配置了什么呢?

前面的文章中介绍过Web应用,抽象来讲就是一个处理HTTP请求并返回HTTP响应的程序。这个程序的处理过程大致可以描述成:

  1. 将HTTP请求反序列化成一个ServletRequest对象

  2. 根据ServletRequest的信息,找到能正确处理该请求的某个@RequestMapping方法

  3. ServletRequest对象的信息解析成@RequestMapping方法的各种类型的入参

  4. 调用@RequestMapping方法,得到类型为ModelAndView的结果

  5. 根据ModelAndView的内容,填充ServletResponse对象

  6. ServletResponse对象序列化成HTTP响应返回给浏览器

在Spring Web MVC中,对上述的过程进行了设计和抽象。比如:通过request信息查找handler方法的HandlerMapping;根据ModelAndView对象组装HTTP响应的ViewResolver;从request解析出各种方法入参的HttpMessageConverter……

上述过程涉及的概念,Spring Web MVC均提供了默认的实现。@EnableWebMvc注解则是告诉Spring Web MVC使用这些默认实现完成应用的初始化。当然,用户也可以对当中的一些或者全部做个性化的设置。这就是@EnableWebMvc注解的自动配置功能。

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

上面是@EnableWebMvc的源代码,通过使用@Import(DelegatingWebMvcConfiguration.class)注入了DelegatingWebMvcConfiguration类。

DelegatingWebMvcConfiguration继承自WebMvcConfigurationSupportWebMvcConfigurationSupport是实现Spring Web MVC自动配置的核心类,它包含了很多方法,但大部分方法的处理方式都很类似。

WebMvcConfigurationSupport通过定义@Bean方法向WebApplicationContext容器中注入Bean对象。这些@Bean方法会优先提供一套默认的配置,然后调用DelegatingWebMvcConfiguration中的具体方法进行个性化设置。这样的模式既保证了Spring Web MVC框架可以提供一套默认的标准配置,也同时支持开发人员根据框架约定进行个性化设置。下面是一个简化版本的示例:

public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware
  ...
  @Bean
  public ViewResolver mvcViewResolver(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager) {
    // 提供默认的一套配置
    ViewResolverRegistry registry = new ViewResolverRegistry(contentNegotiationManager, this.applicationContext);
    // 调用子类方法,进行个性化设置
    configureViewResolvers(registry);
    ...
    return composite;
  }
  ...
}

DelegatingWebMvcConfiguration中引用了一个类型为WebMvcConfigurerComposite的对象。WebMvcConfigurerComposite既是WebMvcConfigurer的代理类,也是WebMvcConfigurer的一个具体实现。

DelegatingWebMvcConfiguration通过setConfigurers(...)方法将IoC容器中的所有WebMvcConfigurer类型的Bean对象注入到了WebMvcConfigurerComposite中。真正处理个性化设置的其实是WebMvcConfigurerComposite类。

@Configuration(proxyBeanMethods = false)
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
 private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
 @Autowired(required = false)
 public void setConfigurers(List<WebMvcConfigurer> configurers) {
  if (!CollectionUtils.isEmpty(configurers)) {
   this.configurers.addWebMvcConfigurers(configurers);
  }
 }
}

总结一下,Spring Web MVC框架中,个性化设置全部是通过代理类WebMvcConfigurerComposite调用IoC容器中所有WebMvcConfigurer类型的Bean对象实现的。开发人员可以设计多个实现了WebMvcConfigurer接口的@Configuration对象,用于对Spring Web MVC启动过程进行个性化设置。开发人员不需要关心这些对象如何被调起,该过程将由Spring Web MVC框架自动处理。

3 WebMvcConfigurer

到这里,我们便可以修改上一篇文章中的配置方法,让AppConfig类实现WebMvcConfigurer接口,这样我们就可以通过实现WebMvcConfigurer接口中定义的特定方法来对Web应用进行个性化。

package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.example.controller")
public class AppConfig implements WebMvcConfigurer  {
}

4 另一种配置方法

根据前面介绍,WebMvcConfigurer会被WebMvcConfigurerComposite加载,WebMvcConfigurerComposite则会被DelegatingWebMvcConfiguration调用,所以调用入口在DelegatingWebMvcConfiguration类上。也就是说,WebMvcConfigurer中的所有操作都可以通过改变DelegatingWebMvcConfiguration相关方法来实现。因此,DelegatingWebMvcConfigurationWebMvcConfigurer更灵活。

我们可以使@Configuration对象直接继承DelegatingWebMvcConfiguration来达到更灵活的配置功能。这时,在@Configuration上就不能再使用@EnableWebMvc了,否则DelegatingWebMvcConfiguration相关的代码会被框架调用两次,产生错误。

package org.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
@Configuration
@ComponentScan(basePackages = "org.example.controller")
public class AppConfig extends DelegatingWebMvcConfiguration {
}

5 结论

好了,Spring Web MVC框架启动配置的所有方法都介绍完了。整个系列主要介绍了四种启动配置方式:

  1. 通过传统的web.xml文件进行Spring Web MVC应用的配置。

  2. 通过使用WebApplicationInitializer + @EnableWebMvc进行Spring Web MVC应用的配置。

  3. 通过使用WebApplicationInitializer + @EnableWebMvc + WebMvcConfigurer进行Spring Web MVC应用的配置。

  4. 通过使用WebApplicationInitializer + DelegatingWebMvcConfiguration进行Spring Web MVC应用的配置。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

镜悬xhs

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

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

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

打赏作者

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

抵扣说明:

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

余额充值