zoukankan      html  css  js  c++  java
  • spingboot静态资源导入探究(很精细)

    源码探究

    原文链接:https://blog.csdn.net/cristianoxm/article/details/112639177
    这篇博文全篇都在讲WebMvcAutoConfiguration类中addResourceHandlers;讲的很精彩;
    静态资源探究就是在看addResourceHandlers中的源码:

    @Override
    		public void addResourceHandlers(ResourceHandlerRegistry registry) {
    			if (!this.resourceProperties.isAddMappings()) {
    				logger.debug("Default resource handling disabled");
    				return;
    			}
    			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    			CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
    			if (!registry.hasMappingForPattern("/webjars/**")) {
    				customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
    						.addResourceLocations("classpath:/META-INF/resources/webjars/")
    						.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    			}
    			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
    			if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(
                              //这里会创建一个ResourceHandlerRegistration对象
                              registry.addResourceHandler(staticPathPattern)
    			  //这里采用了链式编程,是同一个ResourceHandlerRegistration对象;
                              .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                              .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
    			}
    		}
    

    对于这篇博客中稍有疑问的是这两句:

    # 这一句是指:它从resourceProperties属性类中获取到关于缓存的相关值。//我们可以在ymml配置文件中配置一下这个值;
    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
    # CacheControl类是http报文中的通用首部字段,既存在于请求报文中,也存在于响应报文中;//这个不可以在yml中配置,不用管;
    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();.
    

    关于CacheControl博文: https://blog.csdn.net/zhongshanxian/article/details/81289034

    在源码探究中,参与的比较多的有两个类:ResourceHandlerRegistry/ResourceHandlerRegistration

    原文链接:https://blog.csdn.net/andy_zhang2007/article/details/89133798
    一个ResourceHandlerRegistry中存储着一个List;
    一个ResourceHandlerRegistration中存储着:

    private final String[] pathPatterns; //路径模型
    private final List<String> locationValues = new ArrayList(); //路径模型对应的:静态资源的路径地址;
    

    在springboot中如何生成一个HandlerMapping的

    原文链接:https://blog.csdn.net/andy_zhang2007/article/details/99545776

    WebMvcConfigurationSupport类中的  resourceHandlerMapping(....);
    ResourceHandlerRegistry类中的 getHandlerMapping();//方法详解
    //这里新建了并返回了一个ResourceHttpRequestHandler
    ResourceHandlerRegistration类中的 getRequestHandler();
    

    1.通过WebMvcConfigurationSupport类中的 resourceHandlerMapping(....);我们可以了解,如何完整的返回一个静态资源映射处理器==

    2.我们可以看到在resourceHandlerMapping(....)方法中,调用了registry.getHandlerMapping()方法;

    在getHandlerMapping()方法中,先是将ResourceHandlerRegistration迭代出来;此时ResourceHandlerRegistration中的pathPatterns与
    locationValues是N:M的关系;再将pathPatterns遍历,然后调用registration.getRequestHandler()方法;

    在getRequestHandler()方法中,创建了一个ResourceHttpRequestHandler对象,并将当前ResourceHandlerRegistration对象中的locationValues,cacheControl,cachePeriod等属性全部配置到到ResourceHttpRequestHandler中,并返回ResourceHandlerRegistration对象;

    然后在往下看,将ResourceHandlerRegistry对象中的属性全部配置到ResourceHttpRequestHandler对象中,再用一个key为pathPattern,value为ResourceHttpRequestHandler的map将他们以1:n的关系联系在一起。再将该map由一个SimpleUrlHandlerMapping对象包裹起来返回到resourceHandlerMapping中;

    然后再为SimpleUrlHandlerMapping配置一些信息。到此全部结束。

    总结:

    1.在WebMvcAutoConfiguration类中addResourceHandlers(...)中配置好pathPatterns与locationValues之间的对应关系;
    2.在WebMvcConfigurationSupport类中resourceHandlerMapping(...)中
    根据ResourceHandlerRegistry与ResourceHandlerRegistration返回一个HandlerMapping;
    3.HandlerMapping中包含了一个Map;这个Map中包含了pathPattern与ResourceHttpRequestHandler;对应关系为1:1;
    ResourceHttpRequestHandler对象中配置了locationValues;这样就导致了pathPattern与locationValues的对应关系为1:M;
    因为locationValues是一个List<String>类型;
    
  • 相关阅读:
    Photoshop 基础七 位图 矢量图 栅格化
    Photoshop 基础六 图层
    Warfare And Logistics UVALive
    Walk Through the Forest UVA
    Airport Express UVA
    Guess UVALive
    Play on Words UVA
    The Necklace UVA
    Food Delivery ZOJ
    Brackets Sequence POJ
  • 原文地址:https://www.cnblogs.com/hujesse4/p/14758923.html
Copyright © 2011-2022 走看看