Spring Boot核心原理
Spring Boot核心原理
spring-boot-starter-xxx 方便开发和配置
@SpringBootApplication //注解
public class Springbootdemo1Application { public static void main(String[] args) {
//严格意义上执行的是这块代码
SpringApplication.run(Springbootdemo1Application.class, args);
} }
一、 SpringBootApplication注解
注解的功能:参考https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/htmlsingle/#boot-documentation

除了元注解,还有三个注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan( excludeFilters =
{@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
@SpringBootConfiguration 注解

让我们当前的Bean叫给Spring容器进行管理(IOC),让当前类变成配置类,不需要XML文件进行配置(配置类)。
EnableAutoConfiguration注解

@AutoConfigurationPackage
让包中的类以及子包中的类能被自动扫描到Spring 容器中
@Import({AutoConfigurationImportSelector.class})
程序中默认使用的类帮我们找到
AutoConfigurationImportSelector类如下: 里面的selectImports里面,调用了getAutoConfigurationEntry方法。

getAutoConfigurationEntry方法中调用getCandidateConfigurations方法

getCandidateConfigurations方法使用的文件在META-INF/spring.factories

META-INF/spring.factories保存了系统默认加载进来的类。
这个文件的路径如下图:

@ComponentScan注解
通常它会结合@Coponent相关东西进行使用 总结:@SpringBootApplication
结合Spring MVC:
系统可能用到的Bean,帮我们放在了spring.factories 文件夹中
自己需要加载的bean, @Component结合@ComponentScan
二、 SpringApplication.run(Springbootdemo1Application.class, args);
程序启动的时候执行这段代码,
1、寻找内置的Tomcat执行的地方
this.refreshContext(context);
this.refresh(context);
((AbstractApplicationContext)applicationContext).refresh();
this.onRefresh()
onRefresh(ServletWebServerApplicationContext类中)
this.createWebServer();
factory.getWebServer
最终找到内置创建Tomcat的方法

2、@SpringBootApplicaton注解是如何准备类的? 如何理解spring.factories准备的类,然后拿到准备类去创建具体的对象?
SpringApplication.run(Springbootdemo1Application.class, args): 拿到准备类中的文件--> 具体创建对象
以下图创建Tomcat实例为例

spring.factories中有一个与TomcatServletWebServerFactory相关的配置: ServletWebServerFactoryAutoConfiguration

ServletWebServerFactoryAutoConfiguration类如下

可以看到@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})。
然后进入EmbeddedTomcat类

自动配置:auto-configuration
注解在spring.factories中帮你维护好了所谓的全路径
代码执行的过程中你会用到的话,就会寻找对应的类
Spring Boot核心原理的更多相关文章
- Spring Boot 运作原理
Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...
- Spring Boot启动原理解析
Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...
- Spring Boot运行原理
概述 本文主要写了下Spring Boot运行原理,还有一个小例子. Spring4.x提供了基于条件来配置Bean的能力,而Spring Boot的实现也是基于这一原理的. Spring Boot关 ...
- spring boot启动原理步骤分析
spring boot最重要的三个文件:1.启动类 2.pom.xml 3.application.yml配置文件 一.启动类->main方法 spring boot启动原理步骤分析 1.spr ...
- 3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...
- Spring Boot 核心注解与配置文件
@SpringBootApplication注解 Spring Boot项目有一个入口类 (*Application) 在这个类中有一个main 方法,是运行该项目的切入点.而@SpringBootA ...
- 【SpringBoot】15. Spring Boot核心注解
Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...
- Spring Boot 核心配置文件 bootstrap & application
Spring Boot 核心配置文件 bootstrap & application 1.SpringBoot bootstrap配置文件不生效问题 2.bootstrap/ applicat ...
- Spring boot运行原理-自定义自动配置类
在前面SpringBoot的文章中介绍了SpringBoot的基本配置,今天我们将给大家讲一讲SpringBoot的运行原理,然后根据原理我们自定义一个starter pom. 本章对于后续继续学习S ...
随机推荐
- 常用的js加密
https://github.com/hellobajie/AES-of-JavaScript 此为js的 AES加密方式,两个加密文档,可当做扣js必备
- ansible之基础篇(二)
软件相关模块 yum rpm和yum的区别 rpm:redhat package manager yum 可以解决依赖关系 yum 源配置 使用yum下载时需要先下载epel [epel] name= ...
- three.js展示三维模型
1.概要 最近学习Three.js,尝试加载一些3d max导出的obj.stl模型,在展示模型的时候遇到了一些问题,模型的尺寸.位置和旋转角度每次都靠手工调整,非常的不方便,就想着写一个方法来随心所 ...
- 云计算第二阶段shell脚本
pstree #查看进程树 cat /etc/shells #查看系统安装的所有shell解释器 yum -y install ksh ...
- Kotlin数据类深度解析与底层剖析
今天来学习一下全新关于Kotlin的概念---数据类[data class],也是非常有用的东东,下面先来对其进行理论化的了解: 数据类其实跟java的实体类(model)很类似,像Java定义一个P ...
- vuex传值的使用
1.导入vuex import Vuex from 'vuex' Vue.use(Vuex); 2.创建store实例 let store = new Vuex.Store({ state:{ cou ...
- Python通过lxml库遍历xml通过xpath查询(标签,属性名称,属性值,标签对属性)
xml实例: 版本一: <?xml version="1.0" encoding="UTF-8"?><country name="c ...
- LINUX部署TOMCAT服务器
转载声明: http://www.cnblogs.com/xdp-gacl/p/4097608.html 解压tomcat服务器压缩包 配置环境变量 tomcat服务器运行时是需要JDK支持的,所以必 ...
- python - djanog (静态文件)
# 在 setting 文件中的 static ,通过这个方法(别名) 可以拼接到其它文件夹中的文件 # 第一步: 导入 # {% load static %} # 第二步: 查找 static (别 ...
- Hbase节点及原理
1.Hbase架构:Clinet.Zookeeper.Master.RegionServer.Region.Store.MemStore.HLog 1.读过程 2.写过程 https://www.cn ...