Spring Boot 静态资源访问

静态资源访问

在Spring MVC中,对于静态资源都需要开发者手动配置静态资源过滤。Spring Boot中对此也提供了自动化配置,可以简化静态资源过滤配置。

1.默认策略

Spring Boot 中对于Spring MVC的自动化配置都在WebMvcAutoConfiguration类中,因此对于默认的静态资源过滤策略可以从这个类中一窥究竟。

在WebMvcAutoConfiguration类中有一个静态内部类WebMvcAutoConfigurationAdapter,实现了WebMvcConfigurer接口。WebMvcConfigurer接口中有一个方法addResourceHandlers,是用来配置静态资源过滤的。方法在 WebMvcAutoConfigurationAdapter类中得到了实现,部分核心代码如下:

public class WebMvcAutoConfiguration {
...
    ...
	@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
			addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
				registration.addResourceLocations(this.resourceProperties.getStaticLocations());
				if (this.servletContext != null) {
					ServletContextResource resource = new ServletContextResource(this.servletContext, 		SERVLET_LOCATION);
					registration.addResourceLocations(resource);
				}
			});
		}
    ...
...

Spring Boot在这里进行了默认的静态资源过滤配置,其中 staticPathPattern 默认定义在WebMvcProperties 中,定义内容如下:

private String staticPathPattern = "/**";

this.resourceProperties.getStaticLocations()获取到的默认静态资源位置定义在ResourceProperties代码如下:

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

在getResourceLocations方法中,对这4个静态资源位置做了扩充,代码如下:

static String[] getResourceLocations (String[] staticLocations){
String[] locations = new String[staticLocations.length+ SERVLET_LOCATTONs.length]System.arraycopy(staticLocations,0,locations,0,staticLocations.length) ;
System.arraycopy(SERVLET_LOCATIONS,0, locations,staticLocations.length,SERVLET_LOCATIONS. length) ;
return locations;

其中,SERVLET_LOCATIONS的定义是一个{ “/”}。

综上可以看到,Spring Boot 默认会过滤所有的静态资源,而静态资源的位置一共有5个,分别是"classpath:/META-INF/resources/" 、 “classpath:/resources/” 、 “classpath:/static/” 、 “classpath:/public/“以及”/”,也就是说,开发者可以将静态资源放到这5个位置中的任意一个。注意,按照定义的顺序,5个静态资源位置的优先级依次降低。但是一般情况下,Spring Boot项目不需要 webapp目录,所以第5个"/"可以暂不考虑。

在一个新创建的Spring Boot项目中,添加了spring-boot-starter-web 依赖之后,在resources目录下分别创建4个目录,4个目录中放入同名的静态资源

在这里插入图片描述

如果开发者使用IntelliJ IDEA创建Spring Boot项目,就会默认创建出 classpath:/static/目录,静态资源一般放在这个目录下即可。

2. 自定义策略

如果默认的静态资源过滤策略不能满足开发需求,也可以自定义静态资源过滤策略,自定义静态资源过滤策略有以下两种方式:

2.1 在配置文件中定义

可以在 application.properties 中直接定义过滤规则和静态资源位置,代码如下:

spring.mvc.static-path-pattern=/ static/ **
spring.resources.static-locations=classpath: /static/

过滤规则为/static/**,静态资源位置为classpath:/static/。

重新启动项目,在浏览器中输入“http:/localhost:8080/static/p1.png”,即可看到classpath:/stat目录下的资源。

2.2 Java编码定义

也可以通过Java编码方式来定义,此时只需要实现WebMvcConfigurer 接口即可,然后实现该接口的addResourceHandlers方法,代码如下:

@Configuration
public class MywebMvcConfig implements webMvcConfigurer {
    @override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}

重新启动项目,在浏览器中输入“http:/localhost:8080/static/p1.png”,即可看到classpath:/static/目录下的资源。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值