spring boot:启动原理解析

https://www.cnblogs.com/xiaoxi/p/7999885.html

引入starter,starter引入相关依赖,并为我们在ioc容器中配好相关组件(dispatchservlet、视图解析器、文件上传解析器),springbootapplication.run会返回一个ioc容器,可以在这个容器中遍历到这些组件

主程序所在包下的组件都能被扫描到,无需配置之前的包扫描

所有的属性值都有默认的值,和配置文件绑定

springboot的所有自动配置都在spring-boot-autoconfiguration包下

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
这个依赖包含了下面这些依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
其中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
包含了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.8.RELEASE</version>
<scope>compile</scope>
</dependency>
这个依赖包含的这个jar包中有自动配置类

 

这里就是全部场景配置类,只有我们引入了相关场景后,这些类才能生效

自动配置按需加载。

@SpringBootApplication相当于
@SpringBootConfiguration(configuration)作用是表明是一个配置类,用来向容器中注册bean
@EnableAutoConfiguration(import)作用是调用类的无参构造器,创建这个组件,放到容器中
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
@conditional条件装配
@ImportResource("classpat:beans.xml")作用是将xml中的bean注入到容器中,适合老项目迁移
configurationProperties(“mycar”)作用是将配置文件和类中属性值进行绑定,前提是这个类必须是容器中的组件

@Import(AutoConfigurationPackages.Registrar.class)批量注入bean
@AutoConfigurationPackage将某个包下的所有组件注入到容器中,main程序所在的包下
全部加载然后根据条件装配注解进行按需注入

1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
2、调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
3、利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件
4、从META-INF/spring.factories位置来加载一个文件。 默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件 spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
更多内容请参考:https://www.yuque.com/atguigu/springboot/qb7hy2#ynNRo

 

posted @ 2021-07-20 10:42  奔跑的粽子  阅读(50)  评论(0编辑  收藏  举报