3

spring | 静态资源

 1 year ago
source link: https://benpaodewoniu.github.io/2022/11/08/spring14/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

spring | 静态资源

这里说一下 spring boot 静态资源的存放。

  • spring boot
    • 2.7.5

里面有一句

org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration

进入后,有一个方法

public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
} else {
this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
if (this.servletContext != null) {
ServletContextResource resource = new ServletContextResource(this.servletContext, "/");
registration.addResourceLocations(new Resource[]{resource});
}

});
}
}

第 2 行判断是否自己自定义了静态文件路径。

this.addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");

这个是使用 webjar 的方式导入,使用场景非常少,这里不再解释。

this.addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration

其中 this.resourceProperties.getStaticLocations() 表示 new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};

我们一般使用

  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

其中优先级

resources > static > public

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK