Static resources for Spring Boot development
1 simple function
1 Static resource access
1 Static resource directory
As long as static resources are placed on the classpath: /static (or /public or /resources or /META - INF/resource
Access: current project root path / + static resource name
Principle: Static mapping is wide. When the request comes in, first go to the Controller to see if it can be processed. All requests that cannot be processed are handed over to the static resource processor. If the static resource cannot be found, it will respond with a 404 page.
Change the default static resource path:
spring:
mvc:
static-path-pattern: /res/**
resources:
static-locations: [classpath:/haha/]
2 Static resource access prefix
no prefix by default
spring:
mvc:
static-path-pattern: /res/**
Current project + static -path-pattern + static resource name = find under the static resource folder
3 Home Support
index.html under static resource path
- Static resource paths can be configured
- However, the access prefix of static resources cannot be configured. Otherwise, index.html cannot be accessed by default.
spring:
# 这个会导致welcome page功能失效
# mvc:
# static- path- pattern: /res/**
resources:
static-locations: [classpath:/haha/]
4 Custom Favicon
favicon.ico can be placed in the static resource directory.
spring:
# 这个会导致 Favicon 功能失效
# mvc:
# static-path-pattern: /res/*
5 Principles of Static Resource Configuration
SpringBoot starts to load the xxxAutoConfiguration class (automatic configuration class) by default
The automatic configuration class WebMvcAutoConfiguration of the SpringMVC function takes effect
@Configuration(proxyBeanMethods = false)
(aJConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({
Servlet •class, DispatcherServlet.class^ WebMvcConfigurer
@ConditionalOnlvlissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ondened.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({
DispatchenServletAutoConfigunation.class, TaskExecutionA
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
}
Configuration in the container:
@Configuration(proxyBeanMethods = false)
@Impont(EnableWeblvlvcConfiguration.class)
@EnableConfigurationProperties({
WebMvcProperties .class, ResounceProperties.class})
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfig
The relevant attributes of the configuration file are bound to xxx. WebMvcPropertiesspring.mvcN ResourcePropertiesspring.resources
1 The configuration class has only one constructor with parameters
// 有参构造器所有参数的值都会从容器中确定
// ResourceProperties resourceProperties;获耳又和spring.resources绑定的所1有的值的〉
// WebMvcProperties mvcProperties 获取和spring.mvc绑定的所有的值的对象
// ListableBeanFactory beanFactory Spring的beanFactory
// HttpMessageConverters 找到所1有的HttpMessageConverters
// ResourceHandlerRegistrationCustomizer 找到资源处理器的自定义器。=========
// DispatcherServletPath
// ServletRegistrationBean 给应用注册Servlet、Filter
public WebMvcAutoConfigurationAdapter(ResourceProperties resourcePropertie
ListableBeanFactory beanFactory, ObjectProviden<HttpMessageCon
ObjectProvider< ResourceHandlenRegistrationCustomizer>
ObjectProvider<DispatchenServletPath> dispatcherServletPath^
ObjectProvider<ServletRegistrationBean<?>> servletRegistration
this.resourceProperties = resourceProperties;
this.mvcProperties = mvcProperties;
this.beanFactory = beanFactory;
this.messageConvertersProvider = messageConvertersProvider;
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegist
this.dispatcherServletPath = dispatcherServletPath;
this.servletRegistrations = servletRegistrations;
}
2 Default rules for resource handling
public void addResourceHandlers(ResourceHandlerRegistry registry){
if (!this.resourceProperties.isAddMappings()){
logger.debug("Default resource handling disabled");
return;
}
// ...
}
configuration
spring:
# mvc:
# static-path-pattern: /res/**
resources:
add-mappings: false # 禁用所有静态资源规则