經過WebMvcAutoConfiguration發現Spring Boot作了許多自動配置。html
1.自動配置的VeiwResolverjava
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) { ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver(); resolver.setContentNegotiationManager((ContentNegotiationManager)beanFactory.getBean(ContentNegotiationManager.class)); resolver.setOrder(-2147483648); return resolver; }
@Bean @ConditionalOnBean({View.class}) @ConditionalOnMissingBean public BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = new BeanNameViewResolver(); resolver.setOrder(2147483637); return resolver; }
@Bean @ConditionalOnMissingBean public InternalResourceViewResolver defaultViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix(this.mvcProperties.getView().getPrefix()); esolver.setSuffix(this.mvcProperties.getView().getSuffix()); return resolver; }
2.自動配置的靜態資源web
在自動配置類中addResourceHandlers方法中定義瞭如下靜態資源配置,可繼承起抽象父類,覆寫該方法進行擴展。spring
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文件夾下的靜態文件直接映射爲/**。mvc
webjar app
將經常使用的腳本框架封裝在jar包中,把webjar的/META-INF/resources/webjars/下的靜態文件映射爲/webjar/**,能夠經過http://ip:port/webjar/**訪問。框架
3.自動配置的Formatter和Converterthis
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中。spa
4.自動配置的HttpMessageConvertersdebug
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