
MVC配置原理
SpringBoot下的MVC自动配置
SpringBoot会默认进行配置以下内容:
- Inclusion of
ContentNegotiatingViewResolver
andBeanNameViewResolver
beans.//视图解析器 - Support for serving static resources, including support for WebJars (covered later in this document).//静态资源 并支持webjars
- Automatic registration of
Converter
,GenericConverter
, andFormatter
beans.//类型转换器,前端后端传值转换。Formatter可以进行格式转换,如日期转换。 - Support for
HttpMessageConverters
(covered later in this document).//转换Http的请求和相应,如Java对象转换为Json。 - Automatic registration of
MessageCodesResolver
(covered later in this document).//错误代码 - Static
index.html
support. //主页自定义 - Automatic use of a
ConfigurableWebBindingInitializer
bean (covered later in this document).//将请求等封装在JavaBean中
如果想要保持SpringBootMvc的配置并且要添加更多的配置,
- 可以自己新建一个WebMvc的配置类,
- 实现WebMvcConfiguration接口并使用@Configuration注解,
- 重写相应的方法即可。
- 注意这里不用添加@EnableWebMvc注解。
1 |
|
示例:添加自定义视图解析器
在自定义的MVC配置类下添加一个视图解析器类和方法。
方法使用@Bean注解,将方法引入Spring。
1 |
|
在ContentNegotiatingViewResolver类的getCandidateViews方法内部打断点可以获得目前加载的视图列表;
查看结果:
在候选视图解析器中出现自定义的视图解析器。
添加@EnableWebMvc,表示完全使用自定义的WebMvc自定义配置类
添加@EnableWebMvc,表示完全使用自定义的WebMvc自定义配置类。
原理
在WebMvcAutoConfiguration类中,有一个注解
1
表示如果出现WebMvcConfigurationSupport这个类,就不加载自动配置类
@EnableWebMvc会加载WebMvcConfigurationSupport这个类,会导致WebMvcAutoConfiguration自动配置类失效,然后使用被注解的类作为WebMvc配置类。
@EnableWebMvc注解会导入类DelegatingWebMvcConfiguration;
1
2
3
4
5
6
public EnableWebMvc {
}DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport类。
1
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {...}
扩展SpringMVC
使用几个示例表示如何去扩展SpringMVC。
Formatter
使用
可以通过spring.mvc.format.date修改日期格式。
- 默认格式:dd/MM/yyyy
通过spring.mvc.format.dateTime修改日期时间格式。
- 默认格式:yyyy-MM-dd HH:mm:ss
1 | # 自定义配置日期格式化 |
功能原理
在WebMvcAutoConfiguration自动配置类下有一个方法:
1 |
|
这个方法引用了this.mvcProperties.getFormat();
,mvcProperties指向一个WebMVCProperties对象。WebMVCProperties是SpringBoot下WebMvc的配置类。
1 | private final WebMvcProperties mvcProperties; |
进入WebMvcProperties
1 | //省略后的WebMvcProperties |
得知:
可以通过spring.mvc.format.date修改日期格式。
通过spring.mvc.format.dateTime修改日期时间格式。
在自定义配置类中添加自定义视图解析器
1 | //全面扩展 springmvc |
registry.addViewController("/yn").setViewName("test");
将/yn指向视图test。
结果:
其他
实现了视图解析器接口的类,这个类就是视图解析器。