springboot静态资源

源码分析

  • WebMvcAutoConfiguration
    • WebMvcAutoConfigurationAdapter
      • WebMvcProperties mvcProperties
        @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(registry.addResourceHandler(staticPathPattern)
                    .addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
                    .setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
           }
        }
        
      • ResourceProperties resourceProperties;
        /**
         * Path pattern used for static resources.
         */
        private String staticPathPattern = "/**";
        
      • addResourceHandlers(ResourceHandlerRegistry registry)
        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
            "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
        

结论

  1. 在SpringBoot中,我们可以使用以下方式处理静态资源
    • webjars localhost:8080/webjars/
    • /**,public,static,resources localhost:8080/
  2. 优先级
    • resoures > static(默认) > public
posted @   不写代码想写诗的虫子  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示