springboot源码分析之静态资源
导入依赖
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.5.1</version>
</dependency>
进入WebMvcAutoConfiguration(自动装配的配置文件)
找到addResourceHandlers
!this.resourceProperties.isAddMappings() | 是否自定义了配置(如果自定义了配置,则默认的配置失效直接返回) |
---|---|
this.mvcProperties.getStaticPathPattern(); | 得来静态资源路径(private String staticPathPattern = “/**”😉 |
addResourceLocations() | 加上()里的资源地址 |
getResourceLocations(this.resourceProperties.getStaticLocations() | 获得资源resourceProperties配置;类的StaticLocations地址 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { “classpath:/META-INF/resources/”, “classpath:/resources/”, “classpath:/static/”, “classpath:/public/” }; |
结论:因此静态资源可以放的位置为:("/**", classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/)-----其中classpath:/META-INF/resources/就是webjars里的资源

优先级:
classpath:/resources/> classpath:/static/>classpath:/public/>"/**",