SpringBoot 的启动流程

SpringBoot 的启动流程

一、 生成SpringApplication对象
1. webApplicationType = 推测web应用的类型(NODE(普通项目)、SERVLET(Servlet web项目)、 * REACTIVE(响应式项目 flux)
2. 从spring.factories 中获取BootstrapRegistryInitializer对象
3. initializers = 从spring.factories中获取ApplicationContextInitializer对象
4. isteners = 从spring.factories中获取ApplicationListener对象
二、 该方法启动SpringBoot项目
1. 调用run方法–> (new SpringApplication(primarySources)).run(args);
2. 先生成SpringApplication对象,之后调用run方法 primarySource --> 传进去的的类相当于配置类,可以为一个数组。
3. SpringApplicationListener.starting()
4. 创建一个Spring容器
5. ApplicationContextInitializer—>初始化Spring容器
6.SpringApplicationRunListener.contextPrepared()
7. 把传给run方法的的配置类注册成bean
8. SpringApplicationRunListener.contextLoaded()会
9. 解析配置类、扫描、启动Tomcat/Jetty/Undertow
(AutoConfigurationImportSelector、DeferredImportSelector)
10. SpringApplicationRunListener.started()
11. callRunners–>ApplicationRunner、CommandLineRunner
12. SpringApplicationListener.ready()

    /**
     * 生成SpringApplication对象
     *      webApplicationType = 推测web应用的类型(NODE(普通项目)、SERVLET(Servlet web项目)、      * REACTIVE(响应式项目 flux)
     *      从spring.factories 中获取BootstrapRegistryInitializer对象
     *      initializers = 从spring.factories中获取ApplicationContextInitializer对象
     *      listeners = 从spring.factories中获取ApplicationListener对象
     *
     * 该方法启动SpringBoot项目
     *      调用run方法--> (new SpringApplication(primarySources)).run(args);
     *      先生成SpringApplication对象,之后调用run方法   primarySource --> 传进去的的类相当于配             置类,可以为一个数组。
     *      SpringApplicationListener.starting()
     *      创建一个Spring容器
     *      ApplicationContextInitializer--->初始化Spring容器
     *      SpringApplicationRunListener.contextPrepared()
     *      把传给run方法的的配置类注册成bean
     *      SpringApplicationRunListener.contextLoaded()
     *      会解析配置类、扫描、启动               				Tomcat/Jetty/Undertow(AutoConfigurationImportSelector、DeferredImportSelector)
     *      SpringApplicationRunListener.started()
     *      callRunners-->ApplicationRunner、CommandLineRunner
     *      SpringApplicationListener.ready()
     *
     */
  public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = Collections.emptySet();
        this.isCustomEnvironment = false;
        this.lazyInitialization = false;
        //设置applicationContextFactory类型
        this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
        //默认的“无操作”应用程序启动实现
        this.applicationStartup = ApplicationStartup.DEFAULT;
        this.resourceLoader = resourceLoader;//资源
        Assert.notNull(primarySources, "PrimarySources must not be null");
        //传入的配置类
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        //判断SpringBoot项目类型 webApplicationType=NODE、Servlet、Reactive
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        //启动应用初始化器
        this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
        //初始化容器
 this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        //设置监听器
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        //检查主应用程序
        this.mainApplicationClass = this.deduceMainApplicationClass();
 }

public ConfigurableApplicationContext run(String... args) {
        long startTime = System.nanoTime();//记录开始时间
        DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
        ConfigurableApplicationContext context = null;
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);//获得监听器
        listeners.starting(bootstrapContext, this.mainApplicationClass);//执行监听器
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//初始化默认应用参数类、命令行参数
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);//获得系统环境以及配置信息
            this.configureIgnoreBeanInfo(environment);//排除某些bean
            Banner printedBanner = this.printBanner(environment);//获得打印banner的信息
            context = this.createApplicationContext();//创建ApplicationContext即Spring容器
            context.setApplicationStartup(this.applicationStartup);
            this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);//SpringApplicationRunListener.contextPrepared()
            this.refreshContext(context);//刷新应用上下文
            this.afterRefresh(context, applicationArguments);
            Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);//了的获得执行时间
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
            }
            listeners.started(context, timeTakenToStartup);//执行监听器
            //callRunners-->ApplicationRunner、CommandLineRunner
            this.callRunners(context, applicationArguments);
        } catch (Throwable var12) {
            //处理异常
            this.handleRunFailure(context, var12, listeners);
            throw new IllegalStateException(var12);
        }
        try {
            Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
            //执行ready
            listeners.ready(context, timeTakenToReady);
            return context;
        } catch (Throwable var11) {
            this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var11);
        }
    }
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot启动流程可以分为以下几个步骤: 1. 确定应用程序类型。在启动SpringBoot时,首先需要确定应用程序的类型。这可以通过设置启动类的注解来实现,比如使用@SpringBootApplication注解。 2. 创建SpringBoot应用程序上下文。在确定应用程序类型后,SpringBoot会创建一个应用程序上下文(ApplicationContext)对象。这个上下文对象是整个应用程序的核心,包含了所有的配置信息和Bean定义。 3. 加载配置文件。SpringBoot会自动加载并解析应用程序的配置文件,包括application.properties或application.yml等。这些配置文件可以用来配置应用程序的各种属性,如数据库连接、端口号等。 4. 扫描和注册Bean。SpringBoot会扫描应用程序中的所有类,并将符合条件的类注册为Bean。这可以通过@ComponentScan注解来实现,它会扫描指定包及其子包中的所有类。 5. 执行Bean的初始化和依赖注入。在注册Bean后,SpringBoot会执行Bean的初始化操作,并将其依赖的其他Bean注入到其中。这可以通过使用@Autowired注解来实现。 6. 启动应用程序。在完成上述步骤后,SpringBoot启动应用程序。这将导致应用程序开始监听指定的端口,并处理来自客户端的请求。 总而言之,SpringBoot启动流程包括确定应用程序类型、创建应用程序上下文、加载配置文件、扫描和注册Bean、执行Bean的初始化和依赖注入,最后启动应用程序。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [9千字长文带你了解SpringBoot启动过程--史上最详细 SpringBoot启动流程-图文并茂](https://blog.csdn.net/weixin_44947701/article/details/124055713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值