当前位置: 首页 > 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.dtcms.com/a/312641.html

相关文章:

  • 【MySQL】MySQL的安全风险与安装安全风险
  • GraphRAG:基于知识图谱的检索增强生成技术解析
  • OSPF HCIP
  • RAG 中常见的文本分块(chunk)方法及实战代码示例
  • 基于开源AI智能客服、AI智能名片与S2B2C商城小程序的餐饮行业私域流量运营策略研究
  • JavaEE初阶第十三期:解锁多线程,从 “单车道” 到 “高速公路” 的编程升级(十一)
  • 最新Android Studio汉化教程--兼容插件包
  • 日产轩逸全面评测:可靠性高十万公里无大修,科技配置落后
  • Starrocks ShortCircuit短路径的调度
  • c++类和对象(0基础也能懂)
  • ThinkPHP8学习篇(一):安装与配置
  • 力扣 hot100 Day64
  • Unity_数据持久化_XML序列化与反序列化
  • 设计模式学习[17]---组合模式
  • Android 之 RxJava2
  • 电商系统定制开发流程:ZKmall开源商城需求分析到上线全程可控
  • Node.js (Express) + MySQL + Redis构建项目流程
  • C++ 入门基础(3)
  • 从零开始学Express,理解服务器,路由于中间件
  • Ubuntu20.04 Carla安装与和Ros联合仿真
  • OpenCV轻松入门_面向python(第一章OpenCV入门)
  • Kafka——怎么重设消费者组位移?
  • 北京-4年功能测试2年空窗-报培训班学测开-第六十六天
  • 「iOS」————属性关键字底层原理
  • Jupyter Notebook 中高效处理和实时展示来自 OpenCV 和 Pillow 的图像数据探究
  • 网络缓冲区的设计以及C++实现
  • 【Python练习】075. 编写一个函数,实现简单的语音识别功能
  • 项目日记---高并发内存池整体框架
  • 人工智能与医疗健康:AI 助力医疗的新格局
  • 信号传播速度与延时