zoukankan      html  css  js  c++  java
  • SpringBoot对静态资源的映射规则

    先看下静态资源地址的自动配置类

    @ConfigurationProperties(
        prefix = "spring.resources",
        ignoreUnknownFields = false
    )
    public class ResourceProperties {
      // 可以看出静态资源的默认地址就是以下这4个,这个classpath:就是指创建完springboot项目的自动生成resoureces文件
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS
        = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"}; private String[] staticLocations; private boolean addMappings; private final ResourceProperties.Chain chain; private final ResourceProperties.Cache cache; public ResourceProperties() { this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS; this.addMappings = true; this.chain = new ResourceProperties.Chain(); this.cache = new ResourceProperties.Cache(); } }

    接下来我们再来看下mvc映射的自动配置类

    @Configuration(
        proxyBeanMethods = false
    )
    @ConditionalOnWebApplication(
        type = Type.SERVLET
    )
    @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
    @ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
    @AutoConfigureOrder(-2147483638)
    @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
    public class WebMvcAutoConfiguration {

      /**
      * 可以看出在webMvc自动配置配置类中,有一个静态内部类就是webMvc自动配置适配器,这个适配器就是具体实现了
    WebMvcConfigurer接口的自动配置
      * 而在这个适配器中,可以看到导入了ResoueceProperties.class
      */
      @Configuration(
            proxyBeanMethods = false
        )
        @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
        @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
        @Order(0)
        public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
            private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
            private final ResourceProperties resourceProperties;
            private final WebMvcProperties mvcProperties;
            private final ListableBeanFactory beanFactory;
            private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
            final WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
    
            public WebMvcAutoConfigurationAdapter(ResourceProperties resourceProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider, ObjectProvider<WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider) {
                this.resourceProperties = resourceProperties;
                this.mvcProperties = mvcProperties;
                this.beanFactory = beanFactory;
                this.messageConvertersProvider = messageConvertersProvider;
                this.resourceHandlerRegistrationCustomizer = (WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer)resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
            }
         /**
         * 在这个方法中
    所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源,这种方式是maven方式引入的静态资源(js,css)
         * 其余的都是去默认地址找,这个默认地址就是上边那四个地址
         */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
                if (!this.resourceProperties.isAddMappings()) {
                    logger.debug("Default resource handling disabled");
                } else {
                    Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
                    CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
                    if (!registry.hasMappingForPattern("/webjars/**")) {
                        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                    }
    
                    String staticPathPattern = this.mvcProperties.getStaticPathPattern();
                    if (!registry.hasMappingForPattern(staticPathPattern)) {
                        this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
                    }
    
                }
            }
    
            private Integer getSeconds(Duration cachePeriod) {
                return cachePeriod != null ? (int)cachePeriod.getSeconds() : null;
            }
    
            private void customizeResourceHandlerRegistration(ResourceHandlerRegistration registration) {
                if (this.resourceHandlerRegistrationCustomizer != null) {
                    this.resourceHandlerRegistrationCustomizer.customize(registration);
                }
    
            }
        }
    }    
  • 相关阅读:
    ORA-01940: cannot drop a user that is currently connected
    三分钟入门VyOS网络操作系统
    金笛短信猫发短信一段时间后,停止发送
    ORA-01940 无法删除当前已连接的用户之解决方案
    Table is marked as crashed and should be repaire (
    DirectConnect API
    使用 ElasticSearch Aggregations 进行统计分析
    Cocos2d-x学习笔记(四) 布景层的加入移除
    Spark源代码阅读笔记之DiskStore
    一个搜索迷宫出路的程序
  • 原文地址:https://www.cnblogs.com/vegeta-xiao/p/12468321.html
Copyright © 2011-2022 走看看