素材巴巴 > 程序开发 >

springboot静态资源存放路径解析

程序开发 2023-09-04 09:36:09

1.源码分析

 public void addResourceHandlers(ResourceHandlerRegistry registry) {//1.如果静态资源已经被自定义了,则禁用默认资源处理if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");} else {//2.否则就添加静态资源到/webjars/**下,路径是classpath:/META-INF/resources/webjars/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.如何自定义静态资源路径

1.进入WebMvcAutoConfiguration类

2.

 3.

4.

 

3.静态资源可存放目录

1.源码深入

 

 2.总结

http://Localhost:8080/

/后面跟着的所有请求都会被

String[]{"classpath:/META-INF/resources/", 
 "classpath:/resources/",
 "classpath:/static/","classpath:/public/"};

这四个目录接收

 

在springboot中,我们可以使用以下方式处理静态资源

优先级:resources>static(默认)>public


标签:

素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。