9.Springboot-扩展SpringMVC

总结:本文主要写的是怎么扩展Springboot中的SpringMVC的配置和禁用自动配置(换成自己的手动配置),下面的的一段话是从Springboot的官方说明文档中摘出来的一部分内容,并用翻译软件翻译的,若有不对请指出,谢谢。

1、官网原文翻译:

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

如果您希望保留 Spring Boot MVC 定制并进行更多的 MVC 定制(拦截器、格式化程序、视图控制器和其他特性) ,可以添加您自己的 mvc配置类型的@configuration 类,但不要添加@enablewebmvc。

If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

如果你想提供自定义的 requestmappinghandler mapping、 requestmappinghandler adapter 或 exceptionhandlerexceptionmvc 定制,你可以声明一个类型为 WebMvcRegistrations 的 bean,并使用它来提供这些组件的自定义实例。

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

如果你想完全控制 Spring MVC,你可以添加你自己的@configuration 注释@enablewebmvc,或者像在@enablewebmvc 的 Javadoc 中描述的那样添加你自己的@configuration 注释 delegatingwebmvcvc 配置。

Spring MVC uses a different ConversionService to the one used to convert values from your application.properties or application.yaml file. The means that Period, Duration and DataSize converters are not available and that @DurationUnit and @DataSizeUnit annotations will be ignored.Spring MVC 使用不同的 ConversionService 来转换 application.properties 或 application.yaml 文件中的值。这意味着 Period、 Duration 和 DataSize 转换器不可用,并且@durationunit 和@datasizeunit 注释将被忽略。
If you want to customize the ConversionService used by Spring MVC, you can provide a WebMvcConfigurer bean with an addFormatters method. From this method you can register any converter that you like, or you can delegate to the static methods available on ApplicationConversionService.
如果希望自定义 Spring MVC 使用的 ConversionService,可以使用 addformatter 方法提供 webmvcconfigurebean。通过这个方法,您可以注册任何您喜欢的转换器,或者您可以委托给 ApplicationConversionService 上可用的静态方法。
2、扩展 SpringMVC

​ 1) 在Spring MVC 框架的时候(没有用SpringBoot的时候),配置请求跳转到那个页面和那个拦截器来处理那个请求,如下配置

    <mvc:view-controller path="/login" view-name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/login"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>

2)下面是SpringBoot怎么实现上面配置的功能(官网原文有写简单的操作如上):

​ 1.创建一个配置类添加@configuration注解

​ 2.继承WebMvcConfigurerAdapter类重写自动配置的一个方法

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry); 直接调用父类的方法
        //浏览器发送 /login 请求来到 success
        registry.addViewController("/login").setViewName("success");
    }
}

3)这个功能的实现原理是:

​ 1.根据Spring的自动配置可以知道WebMvcAutoConfiguration是SpringMVC的Web自动配置类

​ 2.在做其他自动配置时会导入;@Import(EnableWebMvcConfiguration.class) 代码如下

 @Configuration
	public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
      private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();

	 //从容器中获取所有的WebMvcConfigurer
      @Autowired(required = false)
      public void setConfigurers(List<WebMvcConfigurer> configurers) {
          if (!CollectionUtils.isEmpty(configurers)) {
              this.configurers.addWebMvcConfigurers(configurers);
            	//一个参考实现;将所有的WebMvcConfigurer相关配置都来一起调用; 下面是所有扩展配置生效原理的方法
            	@Override
             // public void addViewControllers(ViewControllerRegistry registry) {
              //    for (WebMvcConfigurer delegate : this.delegates) {
               //       delegate.addViewControllers(registry);
               //   }
              }
          }
	}

​ 3.容器中所有的WebMvcConfigurer都会一起起作用;

效果:SpringMVC 的自动配置和扩展的配置都会起作用。

3、全面接管SpringMVC;

​ 当我们觉得SpringBoot对SpringMVC的自动配置都不需要的时候,我们可以自己配置。(最上面原文的下半部分翻译)

只需在原来的配置类中添加注解@EnableWebMvc;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /login 请求来到 success
        registry.addViewController("/login").setViewName("success");
    }
}

@EnableWebMvc 注解可以让自动配置失效的原理是什么呢?请看下面分析

1)@EnableWebMvc 的核心是如下

@Import(DelegatingWebMvcConfiguration.class) //里面是web的自动配置的一些方法
public @interface EnableWebMvc {

2)@Import(DelegatingWebMvcConfiguration.class) 如下

@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

3)WebMvcConfigurationSupport 导入DelegatingWebMvcConfiguration类的父类主要内容如下

@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class,
		WebMvcConfigurerAdapter.class })
//容器中没有这个组件的时候,这个自动配置类才生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
		ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

4)@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;

5)导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能;

4、如何修改SpringBoot的默认配置

​ 1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就使用户配置的, 如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

​ 2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

​ 3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

苹水相峰

你的打赏是对我最大的肯定

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

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

打赏作者

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

抵扣说明:

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

余额充值