当前位置: 首页 > news >正文

WebMvc自动配置流程讲解

WebMvc自动配置流程讲解

原理分析

  • 启动springMvc内置的视图解析器ContentNegotiatingViewResolverBeanNameViewResolver
    • ContentNegotiatingViewResolver :并不会解析视图本身,而是委托其他的视图解析器进行解析
      • 所有的视图解析器,都会根据返回的视图名称进行解析视图
@Override
@Nullable
public View resolveViewName(String viewName, Locale locale) throws Exception {RequestAttributes attrs = RequestContextHolder.getRequestAttributes();Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());if (requestedMediaTypes != null) {// 获得所有匹配的视图List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes);// 获取最终的视图View bestView = getBestView(candidateViews, requestedMediaTypes, attrs);if (bestView != null) {return bestView;}}

委派给其他解析器进行解析

@Override
protected void initServletContext(ServletContext servletContext) {Collection<ViewResolver> matchingBeans =BeanFactoryUtils.beansOfTypeIncludingAncestors(obtainApplicationContext(), ViewResolver.class).values();if (this.viewResolvers == null) {this.viewResolvers = new ArrayList<>(matchingBeans.size());for (ViewResolver viewResolver : matchingBeans) {if (this != viewResolver) {this.viewResolvers.add(viewResolver);}}}

由以上的代码可以得出结论,它是从Spring IOC容器中获得viewResolver,我们可以自己定制一个viewResolver,ContentNegotiatingViewResolver也会帮我们委派解析

  • BeanNameViewResolver

    • 根据handler方法返回的视图名称 对应到具体视图并解析(定义一个相同名称的类,此类继承一个view(abstractXlsView等等)的接口去生成一个视图)
  • 支持提供静态资源,包括对WebJars的支持

    • WebJars将静态资源放在jar包中进行访问

    • 以前要访问jpg、css、js等这些静态资源文件,需要在web.xml配置,在springboot中不需要配置,只需要放到指定的文件夹中就可以读取出来(约定大于配置)

      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");return;}addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {registration.addResourceLocations(this.resourceProperties.getStaticLocations());if (this.servletContext != null) {ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);registration.addResourceLocations(resource);}});
      }
      

      为什么我们访问http://localhost:8080/image/timssg.jpg也可以访问到图片呢?

    public static class Resources {private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/" };/*** Locations of static resources. Defaults to classpath:[/META-INF/resources/,* /resources/, /static/, /public/].*/private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
    
    • 上述代码表示,当图片位于上面的文件中也是可以被读取出来的
  • 自动注册Converter,GenericConverterFormatterBean类

  • 支持HttpMessageConverters

    • HttpMessageConverters负责http请求和响应的报文处理
  • 自动注册MessageCodesResolver

    • 修改错误下的格式转换出错类型转换出错的错误代码
  • 静态index.html支持

    • 在springboot中可以直接返回html的视图

    • 因为在自动WebMvcAutoConfiguration配置类配置

    • 所以就可以通过在配置文件中完成配置

    @Bean
    @ConditionalOnMissingBean
    public InternalResourceViewResolver defaultViewResolver() {InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix(this.mvcProperties.getView().getPrefix());resolver.setSuffix(this.mvcProperties.getView().getSuffix());return resolver;
    }
    
    @Configuration(proxyBeanMethods = false)
    @ConditionalOnWebApplication(type = Type.SERVLET)
    @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
    @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
    @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class })
    public class WebMvcAutoConfiguration {/*** The default Spring MVC view prefix.*/public static final String DEFAULT_PREFIX = "";/*** The default Spring MVC view suffix.*/public static final String DEFAULT_SUFFIX = "";
    
  • 自动使用ConfigurableWebingingInitializer bean

http://www.lryc.cn/news/608762.html

相关文章:

  • MySQL 索引失效的场景与原因
  • 嵌入式开发学习———Linux环境下IO进程线程学习(二)
  • 04.Redis 的多实例
  • 笔试——Day27
  • 前端面试手撕题目全解析
  • 【数据迁移】Windows11 下将 Ubuntu 从 C 盘迁移到 D 盘
  • Redis——常用指令汇总指南(三)(哈希类型)
  • Odoo OWL前端框架全面学习指南 (后端开发者视角)
  • 三角洲行动ACE反作弊VT-d报错?CPU虚拟化如何开启!
  • GitOps:云原生时代的革命性基础设施管理范式
  • Ubuntu20.04 Carla安装与和Ros联合仿真
  • Ubuntu22.4部署大模型前置安装
  • AI + 云原生:正在引爆下一代应用的技术革命
  • LabVIEW小波变换检测信号断点
  • HCIP笔记(第四章)
  • 悬挂的绳子,它的函数方程是什么样子的?
  • Python Dash 全面讲解
  • 大屏项目展示
  • 基于Springboot+UniApp+Ai实现模拟面试小工具八:管理端基础功能实现
  • RAG与智能体技术全景解析:架构革新、场景落地与未来趋势
  • linux2.6 和 unix-v6 源码实验
  • uni-app学习笔记01-项目初始化及相关文件
  • Java小红书源码1:1还原uniapp_仿小红书源码
  • UniApp 实现顶部固定导航栏 Tab 及滚动变色效果
  • 7.13.B+树
  • io_setup系统调用及示例
  • [AI8051U入门第十五步]W5500实现DHCP自动获取IP
  • UE5的渲染Debug技巧
  • [每周一更]-(第154期):Docker 底层深度剖析:掌控 CPU 与内存资源的艺术
  • Leetcode 12 java