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 ...
随机推荐
- 【服务器部署】Django+宝塔+Nginx_uwsgi部署
:CentOS安装脚本: yum install -y wget && wget -O install.sh http://download.bt.cn/install/install ...
- 解决 supervisor : 无法加载文件 C:\Users\charles\AppData\Roaming\npm\supervisor.ps1
在使用vsCode中运行cnpm install时报错. 解决方法 1.在win10 系统中搜索框 输入 Windows PowerShell,选择 管理员身份运行 2.使用,win+R打开了powe ...
- Python 控制台输出时刷新当前行内容而不是输出新行
需求目标 执行Python程序的时候在控制台输出内容的时候只显示一行,然后自动刷新内容,像这样: Downloading File FooFile.txt [%] 而不是这样: Downloading ...
- 兄弟,你爬虫基础这么好,需要研究js逆向了,一起吧(有完整JS代码)
这几天的确有空了,看更新多快,专门研究了一下几个网站登录中密码加密方法,比起滑块验证码来说都相对简单,适合新手js逆向入门,大家可以自己试一下,试不出来了再参考我的js代码.篇幅有限,完整的js代码在 ...
- Vue路由模块化的实现方法
分享一个Vue路由模块化方法,简单实用,好用到飞起 路由模块化 1.router/index.js 配置路由 import Vue from 'vue' import VueRouter from ' ...
- Datanode 怎么与 Namenode 通信?
在分析DataNode时, 因为DataNode上保存的是数据块, 因此DataNode主要是对数据块进行操作. A. DataNode的主要工作流程 客户端和DataNode的通信: 客户端向Dat ...
- 从原理上理解MySQL的优化建议
从原理上理解MySQL的优化建议 预备知识 B+树索引 mysql的默认存储引擎InnoDB使用B+树来存储数据的,所以在分析优化建议之前,了解一下B+树索引的基本原理. 上图是一个B+树索引示意图, ...
- 修改注册表修改环境变量PATH
因公司办公电脑做了权限设置,不能通过右击“我的电脑”-属性的方式更改环境变量值 摸索找到了通过修改注册表的方式修改PATH值 Win+R 输入regedit打开册表编辑器 HKEY_LOCAL_M ...
- 组件 popup 设计和源码剖析
前言 NutUI 是一套京东风格的移动端 Vue 组件库,生态系统覆盖面广,支持按需加载.主题定制.多语言等,功能强大.目前 40+ 京东项目正在使用,设计精美,风格统一.在开发组件库的过程中,Nut ...
- 记录laravelchina中的微信小程序教程的第四节的安装easy WeChat扩展的报错
composer require "overtrue/laravel-wechat:~5.0 PHP Fatal error: Allowed memory size of 16106127 ...