SpringBoot-02-原理初探之主启动类
2. 原理初探
2.1 pom.xml
父依赖
主要依赖一个父项目,主要管理项目的资源过滤和插件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
点进去,发现还有一个父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
这里才是真正管理SpringBoot应用里所有依赖版本的地方,SpringBoot的版本控制中心;
以后导依赖默认不需要写版本;但是如果导入的包没有在依赖中管理就需要手动配置版本
启动器 spring-boot-starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web
:帮我们导入web模块正常运行所依赖的组件。
需要使用什么功能,就只需要找到对应的启动器即可!
2.2 主启动类
默认的主启动类
//SpringBootApplication:标注这个类是一个SpringBoot的应用
@SpringBootApplication
public class Springboot01HelloApplication {
public static void main(String[] args) {
//将SpringBoot应用启动
SpringApplication.run(Springboot01HelloApplication.class, args);
}
}
注解:
@SpringBootConfiguration:springboot的配置
@Configuration:spting配置类
@Component:说明这也是spring的组件
@EnableAutoConfiguration:自动配置
@AutoConfigurationPackage:自动配置包
@Import(AutoConfigurationPackages.Registrar.class):自动配置‘包注册’
@Import(AutoConfigurationImportSelector.class):自动配置导入
//获取所有的配置
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
获取候选的配置:
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
@SpringBootApplication
作用:标注在某个类上说明这个类是SpringBoot的主配置类
,SpringBoot就运行在这个类的main方法上来启动
SpringBoot应用。
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan (excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
...
}
@ComponentScan
它对应XML配置中的元素。
作用:自动扫描并加载所有符合条件的组件或bean
,将这个bean定义加载到IOC容器中。
@SpringBootConfiguration
作用:标注在类上,表示这是一个springboot的配置类
继续点进去:
@Configuration
public @interface SpringBootConfiguration {
...
}
@Component
public @interface Configuration {
...
}
这里出现的注解及解释:
@Configuration :说明这是一个配置类,配置类就是对应Spring的xml配置文件
;
@Component:说明启动类本身也是Spring中的一个组件
,负责启动应用。
@EnableAutoConfiguration
作用:开启自动配置功能
,即以前我们需要自己配置的东西,现在SpringBoot自动帮我们配置。
点进该注解:
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
...
}
这里出现的注解:
@AutoConfigurationPackage:自动配置包
Registrar.class的作用:将主启动类所在包及所有子包里的所有组件扫描到Spring容器
中。
@Import(Registrar.class)
public @interface AutoConfigurationPackage {
...
}
@import:表示给容器导入一个组件
@Import(AutoConfigurationImportSelector.class):给容器导入组件
;
AutoConfigurationImportSelector:自动配置导入选择器,那么它到底导入了哪些组件的选择器呢?
这个类中有这样一个方法
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
这个方法又调用了SpringFactoriesLoader类的静态方法:
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
继续点击查看loadSpringFactories方法
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
} try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
发现一个多次出现的文件:
spring.factories
,全局搜索它
spring.factories
我们根据源头打开spring.factories,看到很多自动配置的文件;这就是自动配置根源的所在!
以WebMvcAutoConfiguration
为例,打开:
可以看到一个个都是JavaConfig配置类,并且都注入了一些bean。
所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories
配置文件,并将对应的 org.springframework.boot.autoconfigure
包下的配置项,通过反射实例化为对应标注了@Configuration
的JavaConfig形式的IOC容器配置类,然后将这些汇总成为一个实例并加载到IOC容器。
结论
- SpringBoot在启动的时候从类路径
META-INF/spring.factories
中获取EnableAutoConfiguration
指定的值; - 将这些值作为自动配置类导入容器,自动配置类就生效,帮我们自动配置工作;
- 整个J2EE的整体解决方案和自动配置都在
spring-autoconfigure
的jar包中; - 它会给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好;
- 有了自动配置类,免去了我们手动编写配置注入功能组件等工作。
SpringBoot-02-原理初探之主启动类的更多相关文章
- SpringBoot无法书写主启动类的情况之一
首先需要引入 spring-boot-starter-web 依赖[springboot web 项目 启动器 jar包]: 如果使用镜像请确保镜像路径正确,可参看笔者博客园m-yb的maven 安装 ...
- SpringBoot-02 运行原理初探
SpringBoot-02 运行原理初探 本篇文章根据b站狂神编写 pom.xml 2.1.父依赖 其中它主要是依赖一个父项目,主要是管理项目的资源过滤及插件! <parent> < ...
- 狂神说SpringBoot02:运行原理初探
狂神说SpringBoot系列连载课程,通俗易懂,基于SpringBoot2.2.5版本,欢迎各位狂粉转发关注学习. 微信公众号:狂神说(首发) Bilibili:狂神说Java(视频) 未经作 ...
- 2 — springboot的原理
1.初步探索:第一个原理:依赖管理 发现:这里面存放着各种jar包 和 版本号 这也是:我们在前面第一个springboot项目创建中勾选了那个web,然后springboot就自动帮我们导入很多东西 ...
- SpringBoot 02: 初识SpringBoot
1. SpringBoot 产生原因 spring, springmvc框架使用上的一些缺点: 需要使用的大量的配置文件 还需要配置各种对象 需要把使用的对象放入到spring容器中才能使用对象 需要 ...
- SpringBoot之旅第六篇-启动原理及自定义starter
一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...
- SpringBoot源码学习系列之启动原理简介
本博客通过debug方式简单跟一下Springboot application启动的源码,Springboot的启动源码是比较复杂的,本博客只是简单梳理一下源码,浅析其原理 为了方便跟源码,先找个Ap ...
- SpringBoot主程序类,主入口类
主程序类,主入口类 /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication publi ...
- springboot系列(三) 启动类中关键注解作用解析
一.Springboot:请求入口 @SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransactio ...
随机推荐
- MIT 6.828 Lab 1/ Part 2
Exercise 03 - obj/boot/boot.asm 反汇编文件 截取asm部分文件并注释理解 # Set up the important data segment registers ( ...
- Finding the Right EAV Attribute Table
$customer = Mage::getModel('catalog/product'); $entity = $customer->getResource(); $attribute = M ...
- Cirros镜像
Openstack的开发,基本都使用这个image来测试,因为他比较小,只有10M. 镜像介绍 镜像的地址: https://launchpad.net/cirros/trunk/0.3.0/+dow ...
- You are using pip version 10.0.1, however version 20.2.2 is available.
在安装第三方库时,出现如下提示: You are using pip version 10.0.1, however version 20.2.2 is available.You should co ...
- 计算机网络-应用层(2)FTP协议
文件传输协议(FTP,File Transfer Protocol)是Internet上使用最广泛的文件传送协议.FTP提供交互式的访问,允许客户指明文件的类型与格式,并允许文件具有存取权限.它屏蔽了 ...
- 第1篇 Scrum冲刺博客
一.Alpha阶段各成员任务 梁天龙 任务名称 预计工时 编辑历史记录 2 登陆按键设计 3 考勤记录页面 2 人数记录页面 2 学习课程页面 4 建议页面 2 黄岳康 任务名称 ...
- 牛客网PAT练兵场-数字黑洞
题解:循环即可 题目地址:https://www.nowcoder.com/questionTerminal/2e6a898974064e72ba09d05a60349c9e /** * Copyri ...
- muduo源码解析2-AtomicIntegerT类
AtomicIntegerT template<typename T> class atomicTntergerT:public noncopyable { }; 作用: 与std::ao ...
- asyncio系列之Lock实现
import types import select import time import socket import functools import collections class Futur ...
- C# Mongo DB 修改多层嵌套集合中的字段
C# Mongo DB 修改嵌套集合中的字段 虽然c#的mongo 驱动很强大,而且还支持linq,但是一些复杂的操作语句还是比较困难 这里我用Bson实现功能 这是模型(我这里有多层嵌套) publ ...