springboot webmvc 自动配置
来源:互联网 发布:mac硬盘恢复 编辑:程序博客网 时间:2024/05/21 07:58
通过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})@ConditionalOnMissingBeanpublic BeanNameViewResolver beanNameViewResolver() { BeanNameViewResolver resolver = new BeanNameViewResolver(); resolver.setOrder(2147483637); return resolver;}
- InternalResourceViewResolver:主要通过设置前缀、后缀以及控制器方法中返回的视图名字符串得到实际页面。
@Bean@ConditionalOnMissingBeanpublic 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
@Autowiredprivate HttpMessageConverters messageConverters;public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.addAll(this.messageConverters.getConverters());}
如果要新增自定义的HttpMessageConverter,则只要定义一个自己的HttpMessageConverters的Bean,在Bean中注册 定义好的HttpMessageConverter即可。
@Beanpublic 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
- springboot webmvc 自动配置
- springboot webmvc thymeleaf 模板引擎
- spring webMVC配置
- SpringBoot自动配置原理
- AxonFramework,SpringBoot自动配置
- SpringBoot自动配置
- SpringBoot自动配置原理
- SpringBoot 自动配置
- springboot学习----自动配置
- SpringBoot自动配置实现
- SpringBoot 自动配置原理
- SpringBoot--AxonFramework自动配置
- SpringBoot-自动配置1
- SpringBoot-自动配置源码解析
- springboot 禁用mongoDB自动配置
- SpringBoot(非spring-webmvc)访问Mongodb例子
- SpringBoot学习笔记(三) SpringBoot 自动配置原理
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- 420. Strong Password Checker
- 第九节 继承与组合
- 成为一名初级游戏策划
- android 关闭弹出键盘
- 笨鸟先飞——Java基础Map集合学习
- springboot webmvc 自动配置
- ASP.NET基础教程--02
- 《Angular2之TypeScript》
- velocity 获取文件服务器文件偶尔需要刷新才能加载出来的问题
- elk日志分析filebeat配置(filebeat + logstash)
- c语言中sprint的用法
- StringUtils中isEmpty 和isBlank的区别
- xv6源码分析(六):进程调度
- 国外不良资产处置方式