十八、扩展SpringMVC

狂神说Java:https://www.bilibili.com/video/BV1PE411i7CV

1、WebMvcAutoConfiguration类

@Bean
@Override
public FormattingConversionService mvcConversionService() {
   Format format = this.mvcProperties.getFormat();
   WebConversionService conversionService = new WebConversionService(new DateTimeFormatters()
         .dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
   addFormatters(conversionService);
   return conversionService;
}

2、WebMvcProperties类

在这里插入图片描述

3、配置文件修改配置

spring.mvc.format.date=dd-MM-yyyy

4、 官方建议

如果我们要扩展SpringMVC,官方建议我们这样去做。自定义config

/**
 * @Description TODO
 * 如果我们要扩展SpringMVC,官方建议我们这样去做
 * @Author Administrator
 * @Date 2020/12/10 13:43
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * 视图跳转
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/kuang").setViewName("test");
    }

}

官方文档

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.

package com.kuang.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description TODO
 * 如果我们要扩展SpringMVC,官方建议我们这样去做
 * @EnableWebMvc 这玩意就是导入一个类:DelegatingWebMvcConfiguration
 * 从容器中获取所有的WebMvcConfigurer
 * @Author Administrator
 * @Date 2020/12/10 13:43
 */
@Configuration
@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * 视图跳转
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/kuang").setViewName("test");
    }

}

5、@EnableWebMvc

@EnableWebMvc 这玩意就是导入一个类:DelegatingWebMvcConfiguration

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

查看DelegatingWebMvcConfiguration类,发现其继承了WebMvcConfigurationSupport

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

WebMvcAutoConfiguration类有一个注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)。当WebMvcConfigurationSupport这个类不存在(默认),也就是我们没有加注解,会生效。如果我们加了注解,整个WebMvcAutoConfiguration类就会失效。

@Configuration(proxyBeanMethods = false)
@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 {

所以在使用自定义配置类的时候,不要加注解@EnableWebMvc。

在SpringBoot中,有非常多的XXXConfiguration帮助我们进行扩展配置,只要看见了这个东西,我们就要注意了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值