目录

  • 1.日期格式设置
  • 2.扩展视图解析器功能
  • 3.注解@EnableWebMvc
  • 4.小结



1.日期格式设置

  • springBoot中自动配置springMVC还有一个特性为Formatter
  • springdoc 设置 formdata_日期格式


  • 说到格式,首先最让我们头疼就就是日期格式,我们可以去看看springBoot里日期格式默认是什么样的
  • 直接去WebMvcAutoConfiguration的配置类WebMvcProperties中看看日期默认格式
@Deprecated
@DeprecatedConfigurationProperty(replacement = "spring.mvc.format.date")
public String getDateFormat() {
	return this.format.getDate();
}

==========================

public String getDate() {
	return this.date;
}

==========================
/**
 * Date format to use, for example `dd/MM/yyyy`.
 */
private String date;
  • 显然,默认的年月日的格式为:dd/MM/yyyy
  • 在这个默认配置下面还有两个配置
/**
 * Date format to use, for example `dd/MM/yyyy`.
 */
private String date;

/**
 * Time format to use, for example `HH:mm:ss`.
 */
private String time;

/**
 * Date-time format to use, for example `yyyy-MM-dd HH:mm:ss`.
 */
private String dateTime;
  • 我们可以发现springBoot中默认的date(日期)、time(时间)和dateTime(日期时间) 3种格式的默认格式,所以在使用的时候如果我们没有在application中配置我们自定义的对应的格式,我们就必须遵守这3种格式来配置日期数据
  • 这个XXXproperties对应的属性值自动注入注解为@ConfigurationProperties(prefix = “spring.mvc”)

2.扩展视图解析器功能

  • 就是我们自己在定义的有注解@configuration进行标注的,且实现了WebMvcConfigurer的类中覆写WebMvcConfigurer的addViewControllers(),以此为我们的视图新增我们指定的视图跳转规则
  • springdoc 设置 formdata_spring boot_02

  • 这个方法的功能和最初学习springMVC的时候学习controller的实现方法相似:实现controller接口,并且使用mv.setViewName()设置要跳转的视图名称,并且还要在spring容器中设置对应的url和controller的映射
  • springdoc 设置 formdata_spring boot_03


  • springdoc 设置 formdata_spring_04

  • 需要注意的是:这个方法并不是像第16篇博客中定义了一个视图解析器,它的作用只是在现有的视图解析器的基础上,增加了视图跳转映射
  • 具体实现方式就是前端请求的到达controller不变,controller返回指定的视图名称不变,但是视图解析器在返回对应的视图的时候,不会去找controller返回的视图名称的视图,而是在我们定义的这个映射中,找到映射的这个视图,数据渲染之后将视图返回给用户
  • 比如:registry.addViewController("/main.html").setViewName("/dashboard");
  • controller返回 return "redirect:/main.html";,但是视图解析器返回的视图将是dashboard

  • 实现方法addViewControllers()
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/test").setViewName("/index");
}
  • 作用:前端请求localhost:8080/test,返回的视图将会是localhost:8080/index
  • 这个方法的优点就是将原来"实现controller接口,并且使用mv.setViewName()设置要跳转的视图名称"方法中在spring容器中设置url映射步骤放在了方法里面解决
  • 定义这样的一个方法,就可以增强/扩展原来视图解析器的功能,原来默认的视图解析器必须设置对应的controller来映射视图名称才能实现跳转,现在直接在自定义的XXXconfig中就可以实现url和视图的关联,这就可以少写一个controller的方法
  • 测试

springdoc 设置 formdata_spring_05


3.注解@EnableWebMvc

  • 在上一篇博客中我们说到了,如果想要扩展WebMvc的某一个配置,如上一篇博客中扩展了一个自定义的视图解析器,我们只需要定义一个类,实现接口WebMvcConfigurer,并在类上加上注解@Configuration进行标注,并最后重点注明了不要加上注解@EnableWebMvc,这样定义的一个类,才是默认的自动配置类WebMvcAutoConfiguration的功能扩展类,它会增强WebMvcAutoConfiguration的功能;如果加上了注解@EnableWebMvc,这个类就变成了替代自动配置类WebMvcAutoConfiguration的替代类,即替代了WebMvcAutoConfiguration
  • 那么这是为什么呢?
  • 查看注解@EnableWebMvc的定义
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
  • 可以发现这个注解除了元注解之外只有一个注解@Import({DelegatingWebMvcConfiguration.class}),其他什么都没有做,所以我们可以说这个注解就是导入了一个类DelegatingWebMvcConfiguration的class对象,作用就是要通过反射实例化这个类
  • 我们再去看看类DelegatingWebMvcConfiguration的定义
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {}
  • 这个类继承了另一个类WebMvcConfigurationSupport
  • 这个类本身没有什么稀奇的,它的作用在我们的WebMvcAutoConfiguration类定义上的注解上
//只截取了重要的定义部分
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
public class WebMvcAutoConfiguration {}
  • 我们可以看到自动配置类WebMvcAutoConfiguration 被加载的又一个条件:OnMissingBean,意思就是没有这个bean,就是没有这个类的实例化对象,这个类就是WebMvcConfigurationSupport
  • 到此我们就明白了为什么在扩展的时候不能使用注解@EnableWebMvc,因为它会导入类WebMvcConfigurationSupport的子类DelegatingWebMvcConfiguration,这样容器中就会存在WebMvcConfigurationSupport对象,那么我们的自动配置类WebMvcAutoConfiguration 就不会被加载到spring容器中,结果就是原来自动配置的webmvc环境都不会自动配置了,全部需要我们手动的进行配置

springdoc 设置 formdata_spring_06

  • 可见它也继承了类DelegatingWebMvcConfiguration,这样不就进一步证实了我们自定义的标准了注解@EnableWebMvc的类是用来代替我们自动配置类WebMvcAutoConfiguration 的了吗?

4.小结

  • 在springBoot中,有很多的xxxConfig,注意:不是xxxAutoConfiguration ,这些类上面只要没有添加注解@Enablexxx,那它内部的bean就都是在帮助我们的xxxAutoConfiguration进行功能的扩展/扩充
  • 如果加上了注解@Enablexxx,这个类的bean配置就是在代替原来的xxxAutoConfiguration的自动配置
  • 所以我们如果在springBoot相关项目中的代码中看到了xxxConfig,应该马上看看标注它的注解,判断这个类是在扩展自动配置类还是在代替自动配置类,一般很少有代替的
  • 所以我们一般看到xxxConfig之后,主要注意它为原有的xxxAutoConfiguration 增强/扩展了哪些功能即可