注意:该博客都是我自己在学习中的 学习笔记 存在对于学习资料的摘录
首先查看Spring自动给我们配置了那些东西:
在application.properties中开启debug模式
可以在控制台:
Positive matches:下面看到如下图片表示spring自动给我们配置了
WebMvcAutoConfiguration
再查看WebMvcAutoConfiguration给我们配置了什么;
重点看如下:
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class,
org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
这里可以看到,WebMvcProperties和资源文件spring.mvc前缀绑定和ResourceProperties和资源文件spring.resources绑定,WebProperties也是和spring.mvc绑定。
它有一个有参构造器:
public WebMvcAutoConfigurationAdapter(
org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
resourceProperties 就是上面 和资源文件绑定了的 ResourceProperties
webProperties 就是上面的WebProperties
mvcProperties 就是上面的WebMvcProperties
ListableBeanFactory beanFactory 这个是一个 spring的容器工厂
HttpMessageConverters 所有的HttpMessageConverters
ResourceHandlerRegistrationCustomizer 资源自定义处理器
DispatcherServletPath
ServletRegistrationBean 给应用注册 filter,Servlet。
资源处理的默认规则:
这这里if语句判断 ResourceProperties的一个属性:
默认为 true
所以这里我们可以得出一个结论:
add-mappings设置为true 启用静态资源
add-mappings设置为false 禁用静态资源
欢迎页的规则:
这个也是在上面 那个配置类中的方法:
欢迎页处理映射:
我们进去这个类,看到这个构造器:
这里可以看到我们设置了静态资源 我们就找不到欢迎页了
会调用 Controller /index