springboot的启动方法

Java知识点总结:想看的可以从这里进入

1.9、启动运行流程

SpringBoot的启动是通过启动类的main方法来启动的

public static void main(String[] args) {
    SpringApplication.run(SpringbootApplication.class, args);
}

在启动时,首先实例化SpringApplication类,构造器加载初始化器和监听器,然后调用run方法,run方法启动监听器,加载配置文件,装配环境参数,打印banner的图案,获取处理上下文区域,加载并生产bean,处理完成后发布上下文,执行Runner运行器,并返回。

  • SpringApplication类:

    • 判断是普通项目还是web项目。
    • 查找并加载所有可用初始化器,设置到initializers属性中
    • 找出所有的可用的监听器,设置到 listeners 中
    • 找到运行的主类
  • run() 方法

    • 计时器,监听器启动

    • 加载配置文件,装配配置参数

    • 打印banner图案

    • 创建处理上下文,加载并创建bean对象,

    • 发布应用执行Runner运行器,返回上下文

      public ConfigurableApplicationContext run(String... args) {
         StopWatch stopWatch = new StopWatch();
         stopWatch.start();
         DefaultBootstrapContext bootstrapContext = createBootstrapContext();
         ConfigurableApplicationContext context = null;
         configureHeadlessProperty();
         SpringApplicationRunListeners listeners = getRunListeners(args);
         listeners.starting(bootstrapContext, this.mainApplicationClass);
         try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            context.setApplicationStartup(this.applicationStartup);
            prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
               new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
            }
            listeners.started(context);
            callRunners(context, applicationArguments);
         }
         catch (Throwable ex) {
            handleRunFailure(context, ex, listeners);
            throw new IllegalStateException(ex);
         }
      
         try {
            listeners.running(context);
         }
         catch (Throwable ex) {
            handleRunFailure(context, ex, null);
            throw new IllegalStateExction(ex);
         }
         return context;
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

辰 羽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值