通过WebMvcAutoConfiguration发现Spring Boot做了许多自动配置。
1.自动配置的VeiwResolver
- ContentNegotiatingViewResolver:自身不处理view,而是代理给不同的ViewResovler来处理不同的view,优先级最高。
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager((ContentNegotiationManager)beanFactory.getBean(ContentNegotiationManager.class)); resolver.setOrder(-2147483648); return resolver; }
- BeanNameViewResolver:根据controller方法中返回的字符串的view来渲染视图。
@Bean @ConditionalOnBean({View.class}) @ConditionalOnMissingBean public BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = new BeanNameViewResolver(); resolver.setOrder(2147483637); return resolver; }
- InternalResourceViewResolver:主要通过设置前缀、后缀以及控制器方法中返回的视图名字符串得到实际页面。
@Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(this.mvcProperties.getView().getPrefix()); esolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; }
2.自动配置的静态资源
在自动配置类中addResourceHandlers方法中定义了以下静态资源配置,可继承起抽象父类,覆写该方法进行扩展。
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if(!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
Integer cachePeriod = this.resourceProperties.getCachePeriod();
if(!registry.hasMappingForPattern("/webjars/**")) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(cachePeriod));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if(!registry.hasMappingForPattern(staticPathPattern)) {
this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));
}
}
}
- 类路径文件
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
把类路径下的/static、/public、/resources和/META-INF/resources文件夹下的静态文件直接映射为/**。
-
webjar
将常用的脚本框架封装在jar包中,把webjar的/META-INF/resources/webjars/下的静态文件映射为/webjar/**,可以通过http://ip:port/webjar/**访问。
3.自动配置的Formatter和Converter
public void addFormatters(FormatterRegistry registry) {
Iterator var2 = this.getBeansOfType(Converter.class).iterator();
while(var2.hasNext()) {
Converter formatter = (Converter)var2.next();
registry.addConverter(formatter);
}
var2 = this.getBeansOfType(GenericConverter.class).iterator();
while(var2.hasNext()) {
GenericConverter formatter1 = (GenericConverter)var2.next();
registry.addConverter(formatter1);
}
var2 = this.getBeansOfType(org.springframework.format.Formatter.class).iterator();
while(var2.hasNext()) {
org.springframework.format.Formatter formatter2 = (org.springframework.format.Formatter)var2.next();
registry.addFormatter(formatter2);
}
}
只要我们程序中自定义了Converter、GenericConverter和Formatter接口的实现类Bean,这些Bean就会自动注册到Spring MVC中。
4.自动配置的HttpMessageConverters
WebMvcAutoConfiguration中,注册了messageConverters
@Autowired
private HttpMessageConverters messageConverters;
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.addAll(this.messageConverters.getConverters());
}
如果要新增自定义的HttpMessageConverter,则只要定义一个自己的HttpMessageConverters的Bean,在Bean中注册 定义好的HttpMessageConverter即可。
@Bean
public HttpMessageConverters customConverters(){
HttpMessageConverter customConverter1 = new CustomConverter();
HttpMessageConverter customConverter2 = new CustomConverter();
return new HttpMessageConverters(customConverter1 ,customConverter2);
}
5.静态首页的支持
classpath:/META-INF/resources/index.html
classpath:/resources/index.html
classpath:/static/index.html
classpath:/public/index.html