好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

在springboot中添加mvc功能的正确姿势讲解

springboot 添加mvc功能

先放出来几个类(包含注解或接口)来观摩一下

WebMvcConfigurer @EnableWebMvc WebMvcConfigurerAdapter (已过时,不再详述,可以理解为继承该类有和实现WebMvcConfigurer一样的效果) WebMvcConfigurationSupport WebApplicationInitializer

这里只聊springboot或者无web.xml环境的情况,无论如何得看一下这个祖宗,以下代码来源于spring官网

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

public class MyWebApplicationInitializer implements WebApplicationInitializer {

     @Override

     public void onStartup(ServletContext servletCxt) {

         // Load Spring web application configuration

         AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();

         ac.register(AppConfig. class );

         ac.refresh();

 

         // Create and register the DispatcherServlet

         DispatcherServlet servlet = new DispatcherServlet(ac);

         ServletRegistration.Dynamic registration = servletCxt.addServlet( "app" , servlet);

         registration.setLoadOnStartup( 1 );

         registration.addMapping( "/app/*" );

     }

}

这个是一切无配置文件spring和springmvc整合的基础,用来替代原始的web.xml中的ContextLoadListener和DispatcherServlet,两者效果等同;

现在我们先基于上述代码的情况来说,请注意以下结论的前提是基于上述示例代码,很重要,其实就像是很久之前我们从零开始搭建整合方案一样,现在只是配置了整合的类,还没有功能,如果我们想要再配置一个json的消息转换器

那么我们就会有如下几种方案

继承 WebMvcConfigurationSupport 实现 WebMvcConfigurer 继承 WebMvcConfigurerAdapter (已过时不再详述) 继承WebMvcConfigurationSupport和实现WebMvcConfigurer的区别如下 WebMvcConfigurationSupport 直接继承并使用@Configuration标识即可,而实现WebMvcConfigurer则需要标识为注解@Configuration以外还需要使用注解@EnableWebMvc标识才可,所以一个项目中可以有多个地方去实现这个接口,只要标识为配置类。然后在一处地方开启@EnableWebMvc就可。这里提前说一下如果是springboot环境这里还大有说头,这里还有个大坑,留在后面说 WebMvcConfigurationSupport 更偏向底层,可以定制化的功能更多,而WebMvcConfigurer是一个接口,是针对WebMvcConfigurationSupport功能将一些常用的功能选择性的暴露出来,实际上WebMvcConfigurer是依赖于WebMvcConfigurationSupport来实现功能添加的

为什么说WebMvcConfigurer是依赖于WebMvcConfigurationSupport来实现功能添加的?我们来看一下配合该接口的注解@EnableWebMvc源码

?

1

2

3

4

5

6

@Retention (RetentionPolicy.RUNTIME)

@Target (ElementType.TYPE)

@Documented

@Import (DelegatingWebMvcConfiguration. class )

public @interface EnableWebMvc {

}

来看一下这个注解导入的配置类DelegatingWebMvcConfiguration为何物?以下摘取该类部分源码

?

1

2

3

4

5

6

7

8

9

10

@Configuration

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);

   }

  }

}

首先这个配置类其实就是我们上面最开始说的用继承来扩展功能的WebMvcConfigurationSupport,那么为什么实现WebMvcConfigurer接口也行?注意看上图中的代码,提供了一个configurers属性, 然后通过setConfigurers方法注入将ioc容器中的所有实现了WebMvcConfigurer接口的配置类都添加到configurers中;后续实现代码不是本章讨论范围,我也没有往下看,单看这里其实就已经明白了。

**上述是整合的基础,那么当我们在springboot中要添加功能的时候要注意一些什么事情呢?**这个也是写这篇文章的目的,因为最近在项目中有人在扩展功能的时候去继承了WebMvcConfigurationSupport这个类,然后联想到之前的项目也有人在springboot项目中使用了注解@EnableWebMvc,这两种情况都不会导致项目启动报错,但却在不该使用的时候使用了这些功能,导致了项目其实是不能正常使用的。现在来看一下为什么?

首先看一下springboot给我们提供的自动整合类,请参考类

?

1

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

我来截取部分代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Configuration

@ConditionalOnWebApplication (type = Type.SERVLET)

@ConditionalOnClass ({ Servlet. class , DispatcherServlet. class , WebMvcConfigurer. class })

@ConditionalOnMissingBean (WebMvcConfigurationSupport. class )

@AutoConfigureOrder (Ordered.HIGHEST_PRECEDENCE + 10 )

@AutoConfigureAfter ({ DispatcherServletAutoConfiguration. class , TaskExecutionAutoConfiguration. class ,

   ValidationAutoConfiguration. class })

public class WebMvcAutoConfiguration {   

    

     @Configuration

  @Import (EnableWebMvcConfiguration. class )

  @EnableConfigurationProperties ({ WebMvcProperties. class , ResourceProperties. class })

  @Order ( 0 )

  public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ResourceLoaderAware {

     }   

      /**

   * Configuration equivalent to {@code @EnableWebMvc}.

   */

  @Configuration

  public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {

     }

}

现在说一下结论,springboot为我们提供的整合功能,已经默认的帮我们添加了很多功能,如消息转换器,静态资源映射,视图解析器,看下WebMvcAutoConfiguration的内部类WebMvcAutoConfigurationAdapter,其实就是实现了接口WebMvcConfigurer,然后再通过注解@Import(EnableWebMvcConfiguration.class)又将EnableWebMvcConfiguration这个配置类导入了进来,而我们点进去发现这个类的作用其实就是等同于之前我们说过的@EnableWebMvc。因此我们说的消息转换器啊,静态资源映射,视图解析器等这些默认实现就在WebMvcAutoConfiguration的内部类WebMvcAutoConfigurationAdapter又通过@Bean注入进来的

也就是说springboot其实帮我们整合好之后又默认帮我们做了一切常用的实现,这样我们开箱即用的不仅是整合好的框架,还有一些约定大于配置的功能,如静态资源要放在static下,其实就是默认帮我们做了资源映射,详细可以看下

?

1

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter#addResourceHandlers

默认整合的功能基本满足于我们日常的开发,而如果我们还需要添加功能要怎么办呢?其实就是直接实现接口WebMvcConfigurer然后将当前类使用注解@Configuration标识为配置类即可。

那么为什么不能再继续继承接口WebMvcConfigurationSupport了呢?还是来看一下我们的自动配置类WebMvcAutoConfiguration吧,仔细看一下上面的配置类上的条件表达式中有这么一句非常非常重要的@ConditionalOnMissingBean(WebMvcConfigurationSupport.class),

上面我们所属的所有整合功能的前提是当前ioc容器中没有WebMvcConfigurationSupport这个bean,我们看到了springboot自己的实现是在当前自动配置类生效的时候才通过实现接口WebMvcConfigurer的,所以容器中在当前配置类未执行之前也是没有这个WebMvcConfigurationSupport的,现在我们突然在项目中添加功能的时候去继承了这个类,然后标识为配置类之后,立马在容器中就出现了这个bean,然后springboot就会以为我们要全面接管整合springmvc,我们要抛弃它的默认实现,然后自己玩。然后就悲剧了。现在整个mvc中反而只有我们自己新加的这个扩展空间了。这在绝大多数情况下根本不是我们想要的。

还有一个问题,为什么加注解@EnableWebMvc也不行?

其实通过上面的讲解我们已经能够看出来,这个注解其实就是导入了DelegatingWebMvcConfiguration这个配置类,而这个类就是继承WebMvcConfigurationSupport的,这两个效果是相同的,所以也不行。

总而言之一句话,在WebMvcAutoConfiguration这个配置类执行之前,无论是继承WebMvcConfigurationSupport还是在某个配置类上添加了注解@EnableWebMvc,都会导致容器中会被注入类型为WebMvcConfigurationSupport的bean,而springboot在实现自动配置时将这种行为定义成了你要自己去实现``

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/yichen0429/article/details/105970732

查看更多关于在springboot中添加mvc功能的正确姿势讲解的详细内容...

  阅读:23次