Spring Boot Web 开发
2022-11-25 本文已影响0人
Tinyspot
1. 静态资源访问
1.1 静态资源目录
/static, /public, /resources, /META-INF/resources
默认映射路径是 /**
(/**
默认是拦截所有请求)
classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources
image.png
请求地址 http://localhost:8888/public.jpg
1.2 静态资源和动态资源地址一样
请求优先找 Controller 处理,若不能处理,再找静态资源处理器
可以指定拦截的前缀来访问静态资源
2. 配置静态资源地址
2.1 方式一:配置 application.yml
spring:
mvc:
static-path-pattern: /static/** # 指定访问路径前缀
请求地址 http://localhost:8888/static/public.jpg
指定静态资源目录
spring:
mvc:
static-path-pattern: /static/**
web:
resources:
static-locations: classpath:/images,classpath:/static
请求地址 http://localhost:8888/static/demo.jpg
2.2 方式二:继承 WebMvcAutoConfiguration
- Deprecated as of 5.0 WebMvcConfigurer has default methods
- Spring Boot 2.0以后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer接口或者直接继承WebMvcConfigurationSupport类
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 将 /static/** 访问映射到/public/**
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/public/");
// super.addResourceHandlers(registry);
}
}
访问地址 http://localhost:8888/static/public.jpg
2.3 方式三:WebMvcConfigurer
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/img/**")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/")
.addResourceLocations("classpath:/resources/");
}
}
访问地址 http://localhost:8888/img/public.jpg
3. 静态资源配置原理
Spring Boot启动默认加载 xxxAutoConfiguration 类
SpringMVC自动配置类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
@AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
public class WebMvcAutoConfiguration {
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
public void addResourceHandlers(ResourceHandlerRegistry registry) {}
}
}
配置类只有一个有参构造器,有参构造器所有参数的值都会从容器中确定
@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<WebMvcAutoConfiguration.ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
this.resourceProperties = webProperties.getResources();
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
this.mvcProperties.checkConfiguration();
}
}
配置文件相关属性绑定
@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {
}
@ConfigurationProperties("spring.web")
public class WebProperties {
}