Fork me on GitHub

Spring Boot 2.0+ 自定义配置类扩展springMVC的功能

在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js、css等)。

在Spring Boot2.0版本中,WebMvcConfigurerAdapter这个类被弃用了。

@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {

那么我们如何来扩展关于MVC的配置呢?

1.继承WebMvcConfigurationSupport

仔细看一下WebMvcConfigurationSupport这个类的话,可以发现其中有很多add…方法:

/**
* Override this method to add Spring MVC interceptors for
* pre- and post-processing of controller invocation.
* @see InterceptorRegistry
 */
protected void addInterceptors(InterceptorRegistry registry) {
}

/**
 * Override this method to add view controllers.
 * @see ViewControllerRegistry
*/
protected void addViewControllers(ViewControllerRegistry registry) {
}

......

继承WebMvcConfigurationSupport之后,可以使用这些add…方法添加自定义的拦截器、试图解析器等等这些组件。如下:

@Configuration
public class MyMVCConfig implements WebMvcConfigurer {
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login.html").setViewName("login");
    }
}

但这里有个问题就是,当你继承了WebMvcConfigurationSupport并将之注册到容器中之后,Spring Boot有关MVC的自动配置就不生效了。

可以看一下Spring Boot启动WebMVC自动配置的条件:

@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
        ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

其中一个条件就是**@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)**,只有当容器中没有WebMvcConfigurationSupport这个类型的组件的时候,才会启动自动配置。

所以当我们继承WebMvcConfigurationSupport之后,除非你自己对代码把控的相当的好,在继承类中重写了一系列有关WebMVC的配置,否则可能就会遇到静态资源访问不到,返回数据不成功这些一系列问题了。

2. 实现WebMvcConfigurer接口

我们知道,Spring Boot2.0是基于Java8的,Java8有个重大的改变就是接口中可以有default方法,而default方法是不需要强制实现的。上述的WebMvcConfigurerAdapter类就是实现了WebMvcConfigurer这个接口,所以我们不需要继承WebMvcConfigurerAdapter类,可以直接实现WebMvcConfigurer接口,用法与继承这个适配类是一样的。如:

@Configuration
public class MyMVCConfig implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/aiden").setViewName("success");
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
       WebMvcConfigurer wmc = new WebMvcConfigurer() {
           @Override
           public void addViewControllers(ViewControllerRegistry registry) {
               registry.addViewController("/").setViewName("login");
               registry.addViewController("/index.html").setViewName("login");
           }
       };
       return wmc;
    }

}

这两种方法都可以作为WebMVC的扩展,去自定义配置。区别就是继承WebMvcConfigurationSupport会使Spring Boot关于WebMVC的自动配置失效,需要自己去实现全部关于WebMVC的配置,而实现WebMvcConfigurer接口的话,Spring Boot的自动配置不会失效,可以有选择的实现关于WebMVC的配置。

posted @ 2019-12-31 11:15  Aiden郭祥跃  阅读(721)  评论(0编辑  收藏  举报
";