SpringBoot原理(源码跟踪)

1 起步依赖

起步依赖的原理就是Maven的依赖传递,当我们引入了 spring-boot-starter-web 之后,maven会通过依赖传递特性,将web开发所需的常见依赖都传递下来。

2 自动配置

SpringBoot的自动配置是指当spring容器启动了以后,一些配置类、bean对象自动交给了spring容器保管,不需要手动去声明,大大简化了我们的开发。在我们的起步依赖中就有许多已经在启动spring容器时就交给其管理的类,我们可以直接注入并使用。

那么在spring项目启动的初始化时一开始总共将多少配置类交由了spring容器管理呢。

3 自动配置类查看

我们从spring启动项的关键注解@SpringBootApplication开始挖掘源码来找到配置类。

less 复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

这是@SpringBootApplication注解上标记的注解,我们继续看@EnableAutoConfiguration注解,可以看到注解上有

python 复制代码
@Import({AutoConfigurationImportSelector.class})

@Import是将类导入spring容器的方法之一,还有一种方法是@ComponentScan 组件扫描。

继续看AutoConfigurationImportSelector类:

kotlin 复制代码
public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return NO_IMPORTS;
    } else {
        AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
        return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
    }
}

其中有getAutoConfigurationEntry的方法,也写在AutoConfigurationImportSelector类之中

kotlin 复制代码
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return EMPTY_ENTRY;
    } else {
        AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
        List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
        configurations = this.removeDuplicates(configurations);
        Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
        this.checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = this.getConfigurationClassFilter().filter(configurations);
        this.fireAutoConfigurationImportEvents(configurations, exclusions);
        return new AutoConfigurationEntry(configurations, exclusions);
    }
}

configrations是配置的意思。其中有一个getCandidateConfigurations的方法,是获得候选配置的意思,而后续的代码还有checkExcludedClasses和configurations.removeAll(exclusions)的方法,可以推测出这是一个获得spring所有起步依赖种的配置类的方法,并在后续将和已经导入依赖无关的配置类排除在外的操作。那我们直接去看getCandidateConfigurations方法,应该就能找到springboot的源代码是如何获取所有配置类的了。

arduino 复制代码
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).getCandidates();
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
    return configurations;
}

可以看到断言中出现了非常关键的信息:META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

最终在org.springframework.boot.autoconfigure:3.0.2下找到了该目录

其中包含了142项启动时交由spring容器保管的类

当然,并不是spring一启动就将这些配置类全部一股脑交由spring容器管理,根据getAutoConfigurationEntry方法中可以得知,对于与目前导入依赖没有关系的配置类,他们并不会加入到spring容器之中,这大大提升了spring启动的速度。

相关推荐
Emma歌小白18 分钟前
Microsoft Visual C++ Runtime Library - Assertion failed
后端
江湖十年24 分钟前
在 Go 中为什么推荐使用空结构体作为 Context 的 key
后端·面试·go
这里有鱼汤30 分钟前
我用Python复刻了一段历史牛股K线,居然找到7只走势神似的票!
后端·python
古时的风筝38 分钟前
「独立开发」出海数据库用啥?
前端·后端·程序员
Victor3561 小时前
MySQL(152)为什么要使用唯一索引?
后端
Victor3561 小时前
MySQL(151)什么是MySQL的二级索引?
后端
深一海3 小时前
taro微信小程序的tsconfig.json文件说明
开发语言·后端·rust
brzhang9 小时前
颠覆你对代码的认知:当程序和数据只剩下一棵树,能读懂这篇文章的人估计全球也不到 100 个人
前端·后端·架构
躲在云朵里`10 小时前
SpringBoot的介绍和项目搭建
java·spring boot·后端
喵个咪10 小时前
Golang微服框架Kratos与它的小伙伴系列 - 分布式事务框架 - DTM
后端·微服务·go