springboot启动流程分析

1.将一个工程打包,然后查看jar包中的文件有一个MANIFEST.MF文件:

Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: postgresql-demo
Implementation-Version: 1.0-SNAPSHOT
Start-Class: com.lyr.postgresql.PostgresqlDemoApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.3.10.RELEASE
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher

2.当使用java -jar 去启动这个jar包时从Main-Class实例化指定的JarLauncher类进开始:
在JarLauncher实例化时会创建LaunchedURLClassLoader加载器类,通过它来加载指定文件夹下的类,并找到META-INF/MANIFEST.MF下的Start-Class配置,启动我们自己服务写的main方法,开始启动springboot应用。

  • org.springframework.boot.loader.JarLauncher主要做的事:
    1、最主要就是为了生成一个自定义的LaunchedURLClassLoader类加载器,指定好这个类加载器负责BOOT-INF/classes/和BOOT-INF/lib/下的所有jar包。然后再把这个类加载器放在上下文加载器里(Thread.currentThread().setContextClassLoader(classLoader);),以保证整个Spring启动过程中优先使用这个上下文加载器来加载类。
    2、找到META-INF/MANIFEST.MF下的Start-Class配置(即:Application.class),通过反射启动我们业务代码里的Application.class,并通过反射启动Application.class的main方法,拉起整个Spring容器。

3.接下来就是,SpringApplication.run(PostgresqlDemoApplication.class, args)的启动过程:
主要流程为:

  1. 初始化SpringApplication,其中通过加载spring-boot、spring-boot-autoconfigure、spring-beans包下的spring.factory文件初始化基础的类
  2. 开始启动计时
  3. 启动应用监听
  4. 加载应用配置文件,准备环境
  5. 打印应用banner
  6. 创建应用上下文
  7. 准备上下文
  8. 刷新上下文,扫描三方应用的spring.factory在此步完成,扫描加载bean,启动web服务器
  9. 最后刷新上下文
  10. 停止启动计时
  11. 启动所有上下文的监听
  12. 返回应用上下文对象,启动完成
 public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
            2.1 资源加载
            this.resourceLoader = resourceLoader;
            Assert.notNull(primarySources, "PrimarySources must not be null");
            2.2 将主类放入linkedHastSet当中
            this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
            2.3 应用类型的选择
            this.webApplicationType = WebApplicationType.deduceFromClasspath();
            2.4 设置初始化(构造)setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
            2.5 设置(应用)监听器 
            setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
            2.6 配置应用的主方法所在类
            this.mainApplicationClass = deduceMainApplicationClass();
        }
public ConfigurableApplicationContext run(String... args) {
            3.1 计时器开始
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            ConfigurableApplicationContext context = null;
            Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
            3.2 配置参数
            configureHeadlessProperty();
            3.3 配置运行监听器
            SpringApplicationRunListeners listeners = getRunListeners(args);
            3.4 监听器开始工作
            listeners.starting();
            try {
                ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                3.5准备环境--监听器和应用参数
                ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
                3.6配置忽略的Bean信息
                configureIgnoreBeanInfo(environment);
                3.7打印图案
                Banner printedBanner = printBanner(environment);
                3.8 创建应用上下文
                context = createApplicationContext();
                3.9 异常报告
                exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                        new Class[] { ConfigurableApplicationContext.class }, context);
                3.10 准备上下文
                prepareContext(context, environment, listeners, applicationArguments, printedBanner);
                3.11 刷新上下文
                refreshContext(context);
                3.12 刷新上下文善后
                afterRefresh(context, applicationArguments);
                3.13 计时器结束
                stopWatch.stop();
                if (this.logStartupInfo) {
                    new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
                }
                3.14 启动所有监听器
                listeners.started(context);
                callRunners(context, applicationArguments);
            }
            catch (Throwable ex) {
                3.15 处理运行失败
                handleRunFailure(context, ex, exceptionReporters, listeners);
                throw new IllegalStateException(ex);
            }
    
            try {
                3.16 将应用上下文传递给每一个监听器
                listeners.running(context);
            }
            catch (Throwable ex) {
                handleRunFailure(context, ex, exceptionReporters, null);
                throw new IllegalStateException(ex);
            }
            return context;
        }

参考:Springboot 启动主流程总结
SpringBoot面试题-个人准备

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java框架,用于构建独立的、可执行的、生产级的Spring应用程序。它提供了一个快速、简单的方式来开发和部署应用程序。而在Spring Boot的启动过程中,有以下几个主要的步骤: 1. 加载启动类:Spring Boot应用程序的启动类通常是一个带有`@SpringBootApplication`注解的Java类。在应用程序启动时,会通过`main`方法加载这个启动类。 2. 创建Spring Application对象:Spring Boot会创建一个`SpringApplication`对象,用于启动应用程序。`SpringApplication`是Spring Boot框架的核心类,它负责管理整个应用程序的生命周期。 3. 解析配置信息:在启动过程中,`SpringApplication`会解析`application.properties`或`application.yaml`文件中的配置信息,并将其加载到Spring环境中。这些配置信息可以用来配置应用程序的各个方面,如数据库连接、日志级别等。 4. 创建并配置Spring容器:Spring Boot使用Spring容器来管理应用程序中的各个Bean。在启动过程中,`SpringApplication`会根据配置信息创建并配置一个Spring容器,该容器负责加载和管理应用程序中的所有Bean。 5. 执行自定义逻辑:在Spring Boot的启动过程中,可以添加自定义的逻辑。例如,可以通过实现`CommandLineRunner`接口来在应用程序启动后执行一些初始化操作。 6. 启动应用程序:完成上述步骤后,`SpringApplication`会启动应用程序,并通过Servlet容器(如Tomcat、Jetty等)监听端口,开始接收和处理HTTP请求。 总体而言,Spring Boot的启动流程是一个通过加载启动类、解析配置信息、创建和配置Spring容器的过程。通过Spring Boot的自动配置和快速启动能力,开发者可以更加方便地构建和部署Spring应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值