• SpringBoot之静态资源规则与定制化



    前言

    本文主要介绍关于SpringBoot中Web开发的简单功能。


    一、静态资源访问

    只要静态资源放在类路径下: called /static (or /public or /resources or /META-INF/resources
    访问 : 当前项目根路径/ + 静态资源名
    原理: 静态映射/**。
    请求进来,先去找Controller看能不能处理。不能处理的所有请求又都交给静态资源处理器。静态资源也找不到则响应404页面

    也可以改变默认的静态资源路径,/static,/public,/resources, /META-INF/resources失效。

    resources:
      static-locations: [classpath:/haha/]
    
    • 1
    • 2

    在这里插入图片描述

    现在访问当前工程目录+haha.jpg:(http://localhost:8080/haha.jpg)
    在这里插入图片描述
    访问当前工程目录+c.jpg:(http://localhost:8080/c.jpg)
    在这里插入图片描述

    可以访问到这个图片,且访问static文件夹下的资源却页面报错说明静态资源路径修改成功。
    我们深入源码看一下:

    @ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
    public class ResourceProperties {
    
        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;
        ...
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    发现底层源码就是与前缀为spring.resources配置绑定,然后默认lasspath:/META-INF/resources/","classpath:/resources/", "classpath:/static/", "classpath:/public/"等为静态资源路径。所以我们改变静态资源路径也应该从spring.resources.classpath修改,这就是上诉修改的原理。

    二、静态资源访问前缀

    spring:
      mvc:
        static-path-pattern: /res/**
    
    • 1
    • 2
    • 3

    此时的静态资源访问就是:当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找
    例:http://localhost:8080/res/a.jpg

    三、webjar

    可用jar方式添加css,js等资源文件。
    webjar官网

    例如,添加jquery

    <dependency>
        <groupId>org.webjarsgroupId>
        <artifactId>jqueryartifactId>
        <version>3.6.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    项目启动后,访问地址:http://localhost:8080/webjars/jquery/3.5.1/jquery.js
    后面地址要按照依赖里面的包路径。(你们可能不是)
    在这里插入图片描述

    资源处理的默认规则

    @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();
    			//webjars的规则
                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));
    			}
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    根据上述代码,我们可以同过配置禁止所有静态资源规则:

    spring:
    #  mvc:
    #    static-path-pattern: /res/**
    
      resources:
        add-mappings: false   # 禁用所有静态资源规则
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四、welcome与favicon功能

    1.欢迎页支持

    • 静态资源路径下 index.html。(项目启动后,默认访问项目路径就自动加载静态资源文件夹下的index.html)
      在这里插入图片描述
      • 可以配置静态资源路径
      • 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
    spring:
    #  mvc:
    #    static-path-pattern: /res/**   这个会导致welcome page功能失效
      resources:
        static-locations: [classpath:/haha/]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • controller能处理/index。

    欢迎页处理规则

    	HandlerMapping:处理器映射。保存了每一个Handler能处理哪些请求。	
    
    	@Bean
    		public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
    				FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
    			WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(
    					new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(),
    					this.mvcProperties.getStaticPathPattern());
    			welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
    			welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations());
    			return welcomePageHandlerMapping;
    		}
    
    
    //WelcomePageHandlerMapping的构造方法如下:
    	WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
    			ApplicationContext applicationContext, Optional<Resource> welcomePage, String staticPathPattern) {
    		if (welcomePage.isPresent() && "/**".equals(staticPathPattern)) {
                //要用欢迎页功能,必须是/**路径
    			logger.info("Adding welcome page: " + welcomePage.get());
    			setRootViewName("forward:index.html");
    		}
    		else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
                // 调用Controller  /index
    			logger.info("Adding welcome page template: index");
    			setRootViewName("index");
    		}
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    从构造方法中可以看出欢迎页效果必须满足路径是/**,这也就是为什么设置静态资源前缀后,欢迎页失效的原因:底层要求路径是/**,设置静态资源前缀后,路径变成/static-path-pattern/**。

    2.自定义Favicon

    就是网页标签上的小图标

    在这里插入图片描述
    favicon.ico 放在静态资源目录下即可。

    spring:
    #  mvc:
    #    static-path-pattern: /res/**   这个会导致 Favicon 功能失效
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    五、补充

    • SpringBoot启动默认加载 xxxAutoConfiguration 类(自动配置类)
    • SpringMVC功能的自动配置类WebMvcAutoConfiguration,生效
    @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 {
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 给容器中配置的内容:
      • 配置文件的相关属性的绑定:WebMvcProperties==spring.mvc、ResourceProperties==spring.resources
    @Configuration(proxyBeanMethods = false)
    @Import(EnableWebMvcConfiguration.class)
    @EnableConfigurationProperties({ WebMvcProperties.class, ResourceProperties.class })
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
        ...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    配置类只有一个有参构造器

    有参构造器所有参数的值都会从容器中确定
    public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
    		ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
    		ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
    		ObjectProvider<DispatcherServletPath> dispatcherServletPath,
    		ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
    	this.mvcProperties = mvcProperties;
    	this.beanFactory = beanFactory;
    	this.messageConvertersProvider = messageConvertersProvider;
    	this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
    	this.dispatcherServletPath = dispatcherServletPath;
    	this.servletRegistrations = servletRegistrations;
    	this.mvcProperties.checkConfiguration();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • ResourceProperties resourceProperties;获取和spring.resources绑定的所有的值的对象
    • WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象
    • ListableBeanFactory beanFactory Spring的beanFactory
    • HttpMessageConverters 找到所有的HttpMessageConverters
    • ResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。
    • DispatcherServletPath
    • ServletRegistrationBean 给应用注册Servlet、Filter…

    总结

    以上就是SpringBoot关于web开发的一些静态资源讲解。

  • 相关阅读:
    每日leetcode[删除排序链表中的重复元素]
    简单理解 Sentinel 滑动窗口实现原理
    stm32寄存器开发
    Linux搭建redis调试环境
    mybatis总结
    基于Java解决容量设施选址问题
    [前端]NVM管理器安装、nodejs、npm、yarn配置
    观察者模式的介绍
    FANUC机器人RSR自动运行模式的相关配置和参数设置(图文)
    戏说领域驱动设计(廿二)——聚合
  • 原文地址:https://blog.csdn.net/weixin_62951900/article/details/132947892