SpringBoot实战分析(一) 入门级启动过程

1.启动类

@SpringBootApplication
public class DemoApplication {

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

2.点击run,进入SpringApplication类中,调用

public static ConfigurableApplicationContext run(Class<?> primarySource,
        String... args) {
    return run(new Class<?>[] { primarySource }, args);//断点
}

参数说明:

primarySource:启动类的主类


args:为 { } 空值

3.SpringApplication调用

public static ConfigurableApplicationContext run(Class<?>[] primarySources,
        String[] args) {
    return new SpringApplication(primarySources).run(args);//断点
}

4.SpringApplication调用

public ConfigurableApplicationContext run(String... args) {
    //java 查看时间和性能的类
    //https://blog.csdn.net/pandawang1989/article/details/78246316 
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    //定义配置上下文 是个interface 顶级父类包含BeanFactory
    ConfigurableApplicationContext context = null;
    //定义一个错误集合
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    //定义一个awt的headless
    configureHeadlessProperty();
    //获取一个run监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    //启动监听
    listeners.starting();
    try {
     //应用程序参数 初始化
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                args);
     //创建和配置环境 入参:监听 应用程序参数
     ConfigurableEnvironment environment = prepareEnvironment(listeners,
                applicationArguments);
        //配置bean的忽略信息 扫描 spring.beaninfo.ignore 可选true/fale
        //在application.yml中配置
        configureIgnoreBeanInfo(environment);
        //打印环境信息
        Banner printedBanner = printBanner(environment);
        //重点 重点 重点 说三遍
        //创建app的上下文 初始化bean等操作都在这 后面再说
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(
                SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        //准备 使用上下文
        prepareContext(context, environment, listeners, applicationArguments,
                printedBanner);
        //刷新上下文  这个也是  重点 重点  重点
        //synchronized (this.startupShutdownMonitor) 
        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, exceptionReporters, listeners);
        throw new IllegalStateException(ex);
    }

    try {
        //发布事件
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

所有写有注释的地方都断点跟踪的。





猜你喜欢

转载自blog.csdn.net/qq_30653841/article/details/80654069