Spring Boot 自动配置及访问静态资源原理

一、自动配置

Spring Boot 启动类上有个注解: @SpringBootApplication,其实这是一个组合注解,由@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan组成。
在这里插入图片描述
@SpringBootConfiguration其实可以认为是@Configuration的一个升级版版,用来区别spring framework 和 spring boot的,而@ComponentScan在spring framework就支持的,所以重点关注@EnableAutoConfiguration,
可以看到这个注解引入了AutoConfigurationImportSelector和@AutoConfigurationPackage
在这里插入图片描述
这里关注AutoConfigurationImportSelector,他实现了N多类,如下:
在这里插入图片描述
其中需要关注的是DeferredImportSelector,他继承了ImportSelector,然后在spring初始化过程中会调用到,具体看图:
在这里插入图片描述
只要断点在org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry中,就可以看到上图中的调用过程。
下面列举一下上图中主要的几个环节:

org.springframework.boot.SpringApplication#run()  ->
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors()  ->
org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions()  ->
org.springframework.context.annotation.ConfigurationClassParser#parse()  -> 
org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorHandler#process()  ->
org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorGroupingHandler#processGroupImports()  ->
org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorGrouping#getImports()  ->
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.AutoConfigurationGroup#process()  ->
org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#getAutoConfigurationEntry()

重点看getAutoConfigurationEntry中做了什么事:
在这里插入图片描述
经过一番操作,剩下23个自动配置类:

org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration
org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration
org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration
org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration
org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration

到这里基本上就能看明白自动配置是怎么回事了。

二、访问静态资源

如下:resources/static/ 目录下有个index.html,没有在代码中手动配置的情况下,直接访问http://localhost:8080/ ,可以看到跳转到了index.html,这是为什么?
在这里插入图片描述
原来在org.springframework.boot.autoconfigure.web.ResourceProperties中定义了一个属性,这个属性指定了默认的CLASSPATH_RESOURCE_LOCATIONS,如下:
在这里插入图片描述
在讲自动配置的时候,剩下的23个自动配置类中有个org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,在这个类中,spring boot做了与spring mvc相关的一些默认配置,在应用启动过程中获取欢迎页面时,会调用org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.EnableWebMvcConfiguration#getWelcomePage,可以看到正好取到了上面配置的四个默认路径,如下:
在这里插入图片描述
正因为spring boot 默认设置了资源路径,所以可以在不手动配置的情况下就能访问index.html。
如果将/static/ 从默认配置中去掉,再访问页面时,会看到报错,如下:
在这里插入图片描述
说白了,spring boot 的这种默认配置就类似于配置了视图解析器和(或)资源映射器,如果开发人员想访问自定义页面,那么可以做如下配置:

package org.springframework.boot.francis.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("classpath:/page/", ".html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("classpath:/page/**")
                .addResourceLocations("classpath:/page/");
    }
}

效果如下:
在这里插入图片描述
更多与spring mvc相关的用法请参考: https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/spring-boot-features.html#boot-features-developing-web-applications

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值