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;
      }
      

猜你喜欢

转载自blog.csdn.net/yuandfeng/article/details/129709394