springboot提供了默认的静态资源目录
- classpath:/META-INF/resources
- classpath:/resources
- classpath:/static
- classpath:/public
如果我们需要自定义静态资源访问目录该怎么办呢?
springboot的早期版本,是靠继承WebMvcConfigurerAdapter类,重写addInterceptors方法来实现的
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Resource private PermissionInterceptor permissionInterceptor; @Resource private CookieInterceptor cookieInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { //registry.addInterceptor(permissionInterceptor).addPathPatterns("/**"); registry.addInterceptor(cookieInterceptor).addPathPatterns("/**"); super.addInterceptors(registry); } }
新版本的springboot,也就是2.0之后,WebMvcConfigurerAdapter类被标记了@Deprecated,那么我们可以使用WebMvcConfigurer类
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //访问路径 registry.addResourceHandler("/statics/**") //映射真实路径,末尾必须加"/",不然映射不到 .addResourceLocations("classpath:/statics/"); } }