springboot配置静态资源映射

先来一段代码吧,看一下注释

package pers.jeanye.seckill.demo.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import pers.jeanye.seckill.demo.HttpInterceptor;

/**
 * 告诉大家一个天大的秘密,一旦用了代码配置,properties配置就不起效果了
 */
//@Configuration
@EnableWebMvc
@SpringBootConfiguration // 百度了一下,他们说是一样的
public class SpringMVC implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置资源映射
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有请求
        registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/**");
    }
}

先使用properties配置,并注释掉上面的配置,发现可以访问
springboot配置静态资源映射_第1张图片
之后将注释拿掉,还要删掉资源配置,发现无法访问
springboot配置静态资源映射_第2张图片
最后取消properties配置,改用代码,可以访问
springboot配置静态资源映射_第3张图片

你可能感兴趣的:(springboot配置静态资源映射)