SpringBoot执行流程解析

核心理念

  1. 能够帮助开发者快速的整合第三方框架
    原理:maven依赖整合和自定义starter
  2. 完全去除xml配置,采用注解形式
    原理:SpringBoot根据spring体系原生的注解实现包装
  3. 不需要外部容器,内嵌入服务器
    原理:Java语言支持创建Tomcat服务器

自定义starter

Starter是springboot中的一个非常重要的概念,starter相当于模块,它能够将模块所需的依赖整合起来并对模块内的Bean环境进行自动配置,使用者只需要依赖相关功能的starter,无需过多的配置和依赖。
官方命名格式为:spring-boot-starter-{name},以spring-boot-starter开头
自定义的starter的命名格式:{name}-spring-boot-starter
下面自定义 token-redis-spring-boot-starter

Maven依赖
  <!--  springboot-parent -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--  能够给开发者引入该jar包后配置文件的时候有一定提示 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
    </dependencies>
配置configurattion类
package com.mayikt.config;

import com.mayikt.propeties.TokenProperty;
import com.mayikt.service.TokenService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @Description:
* @Author: ChenYi
* @Date: 2020/07/16 08:05
**/
@Configuration
@EnableConfigurationProperties(TokenProperty.class)
public class TokenRedisConfiguration {
  @Bean
  public TokenService tokenService() {
      return new TokenService();
  }
}

相关的配置属性类
package com.mayikt.propeties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @Description:
 * @Author: ChenYi
 * @Date: 2020/07/16 08:07
 **/
@ConfigurationProperties(prefix = "mayikt")
public class TokenProperty {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

package com.mayikt.service;

import com.mayikt.propeties.TokenProperty;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description:
 * @Author: ChenYi
 * @Date: 2020/07/16 08:09
 **/

public class TokenService {
    @Autowired
    private TokenProperty tokenProperty;

    public String  token() {
        return tokenProperty.getUsername() + ">>" + tokenProperty.getPassword();
    }
}

META-INF/spring.factories 内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mayikt.utils.TokenAutoConfiguration

SpringBoot启动流程

核心分为两个步骤

  1. 创建SpringApplication对象
  2. 调用SpringApplication run实现启动同时返回当前的容器上下文

具体的路程

  1. 创建SpringApplication对象Springboot容器初始化操作

  2. 获取当前的启动类型,原理:判断当前classpatch是否有加载我们的servlet类,返回servletweb启动方式
    webApplicationType分为三种类型
    1.响应式启动
    2.None不会嵌入web容器启动
    3.servlet基于web容器启动

  3. setInitializers读取springboot包下的META-INF/spring.factories获取对应的ApplicationContextInitializer装配到集合中

  4. setListeners读取springboot包下的META-INF/spring.factories获取对应的ApplicationListener装配到集合中

  5. mainApplicationClass获取当前运行的主函数

  6. 调用SpringApplication run方法实现启动

  7. StopWatch stopWatch = new StopWatch(),记录我们SpringBoot项目启动时间

  8. getRunListeners(args),读取我们的META-INF/spring.factories中的SpringApplicationRunListeners类型存入到集合中

  9. listeners.starting();循环调用监听starting方法

  10. ConfigurableEnvironment environment = prepareEnvironment(listeners,
    applicationArguments);
    isteners.environmentPrepared(environment);
    读取我们配置文件到SpringBoot容器中

  11. Banner printedBanner = printBanner(environment); 打印我们SpringBoot Banner
    
  12. 建SpringBoot上下文AnnotationConfigServletWebServerApplicationContext对象

  13. refreshContext(context); 刷新我们上下文

  14. 开始创建我们的tomcat容器
    在这里插入图片描述

  15. 开始加载我们的SpringMVC
    在这里插入图片描述

  16. afterRefresh 定义一个空的模版给其他子类实现重写

  17. listeners.started(context); 使用广播和回调机制通知监听器已SpringBoot容器启动成功

  18. listeners.running(context); 使用广播和回调机制通知监听器已SpringBoot容器启动成功running

  19. 最后的返回当前上下文

classpath 与file读取有那些区别

Classpath 读取编译后的配置文件内容
File读取编译前的配置文件
默认读取配置文件名称为:
application
SpringBoot支持 yml yaml xml properties

参考:蚂蚁课堂

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值