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

简介: 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/]

现在访问当前工程目录+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;
    ...
}

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

二、静态资源访问前缀

spring:
  mvc:
    static-path-pattern: /res/**

此时的静态资源访问就是:当前项目 + static-path-pattern + 静态资源名 = 静态资源文件夹下找

例:http://localhost:8080/res/a.jpg

三、webjar

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

webjar官网

例如,添加jquery

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.6.4</version>
</dependency>

项目启动后,访问地址: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));
      }
    }

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

spring:
#  mvc:
#    static-path-pattern: /res/**
  resources:
    add-mappings: false   # 禁用所有静态资源规则

四、welcome与favicon功能

1.欢迎页支持

  • 静态资源路径下 index.html。(项目启动后,默认访问项目路径就自动加载静态资源文件夹下的index.html)
  • 可以配置静态资源路径
  • 但是不可以配置静态资源的访问前缀。否则导致 index.html不能被默认访问
spring:
#  mvc:
#    static-path-pattern: /res/**   这个会导致welcome page功能失效
  resources:
    static-locations: [classpath:/haha/]
  • 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");
    }
  }

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

2.自定义Favicon

就是网页标签上的小图标

favicon.ico 放在静态资源目录下即可。

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

五、补充

  • 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 {
    ...
}
  • 给容器中配置的内容:
  • 配置文件的相关属性的绑定: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 {
    ...
}

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

有参构造器所有参数的值都会从容器中确定
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();
}
  • ResourceProperties resourceProperties;获取和spring.resources绑定的所有的值的对象
  • WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象
  • ListableBeanFactory beanFactory Spring的beanFactory
  • HttpMessageConverters 找到所有的HttpMessageConverters
  • ResourceHandlerRegistrationCustomizer 找到 资源处理器的自定义器。
  • DispatcherServletPath
  • ServletRegistrationBean 给应用注册Servlet、Filter…

总结

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

相关文章
|
4月前
|
移动开发 Java HTML5
Springboot web静态资源配置
Springboot web静态资源配置
57 0
|
5月前
|
XML Java 数据格式
SpringBoot中yaml格式、语法规则及数据读取方式(3种)
SpringBoot中yaml格式、语法规则及数据读取方式(3种)
214 0
|
5月前
|
前端开发 JavaScript Java
Springboot 使用thymeleaf 服务器无法加载resources中的静态资源异常处理
Springboot 使用thymeleaf 服务器无法加载resources中的静态资源异常处理
185 0
|
7月前
|
Java Spring
springboot中的静态资源规则~
springboot中的静态资源规则~
|
4月前
|
Java
springboot WebMvcConfigurer详解自定义配置请求静态资源
springboot WebMvcConfigurer详解自定义配置请求静态资源
|
5月前
|
Java
Springboot配置静态资源
Springboot配置静态资源
20 1
|
5月前
|
缓存 前端开发 JavaScript
SpringBoot中静态资源映射处理
SpringBoot中静态资源映射处理
44 0
|
8月前
|
XML 前端开发 JavaScript
SpringBoot中静态资源访问
SpringBoot中静态资源访问
122 0
|
9月前
|
Java Spring
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
Spring Boot入门(十三) 之 thymeleaf一些常用语法规则
|
Java Spring
SpringBoot静态资源处理(九)下
SpringBoot静态资源处理(九)
247 0
SpringBoot静态资源处理(九)下