• 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);
                }
    
            }
        }
    }    
  • 相关阅读:
    【Python必学】Python爬虫反爬策略你肯定不会吧?
    SpringBoot_日志-切换日志框架
    dev、test和prod是什么意思
    SpringBoot_日志-指定日志文件和日志Profile功能
    SpringBoot_日志-SpringBoot默认配置
    SpringBoot_日志-SpringBoot日志关系
    SpringBoot_日志-其他日志框架统一转换为slf4j
    SpringBoot_日志-日志框架分类和选择
    SpringBoot_配置-@Conditional&自动配置报告
    gcc系列工具 介绍
  • 原文地址:https://www.cnblogs.com/vegeta-xiao/p/12468321.html
Copyright © 2020-2023  润新知