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
    评论
Spring Boot的启动流程可以分为以下几个步骤: 1. 应用主入口:Spring Boot的应用主入口是一个标有@SpringBootApplication注解的类。在启动过程中,它会被作为启动类加载到内存中。 2. 配置形式:Spring Boot提供了多种配置Bean的形式。首先是通过定义Bean的方式,在应用主入口类中使用@Bean注解来定义Bean。其次是通过@Configuration类配置方式,在应用主入口类外创建一个专门用于配置Bean的类,并在该类中使用@Bean注解来定义Bean。还有一种方式是通过Spring XML配置文件进行配置。最后,还可以通过自动配置类来配置Bean,这些自动配置类是Spring Boot内部提供的,会根据配置文件和依赖自动完成一些配置工作。 3. 启动流程:在启动阶段,Spring Boot会依次执行以下步骤: - 加载Spring Boot的核心配置文件和依赖的配置文件。 - 创建并初始化Spring的ApplicationContext容器。 - 执行各个自动配置类,完成自动配置工作。 - 执行应用主入口类中的初始化方法,并启动Spring Boot应用。 4. Bean定义加载顺序:在Spring Boot启动过程中,Bean的加载顺序非常重要。如果在主线程加载Bean的同时,有异步线程进行Dubbo调用或加载Bean,可能会导致死锁。为了避免这种情况,应该保证只有一个线程在进行Spring加载Bean的操作。可以在Spring启动完成后再进行异步初始化操作,或者使用Spring的事件机制,在订阅ApplicationStartedEvent事件后再执行异步初始化操作。 综上所述,Spring Boot的启动流程包括应用主入口、配置形式、启动流程和Bean定义加载顺序。在启动过程中,需要注意Bean的加载顺序,以避免死锁情况的发生。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值