首页  

springboot2启动过程简单分析     所属分类 springboot 浏览量 1546
springboot 2.0.0.RELEASE

spring-boot-starter
spring-boot-starter-web


@RestController
@SpringBootApplication
public class Hello{
	public static void main(String[] args) {
		ConfigurableApplicationContext ctx = null;
		ctx = SpringApplication.run(Hello.class, args);
		System.out.println(ctx);
		// org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
		System.out.println(ctx.getClass());
	}
}

核心  SpringApplication  实例方法
public ConfigurableApplicationContext run(String... args) 


public ConfigurableApplicationContext run(String... args) {
        //1.初始化计时器
        StopWatch stopWatch = new StopWatch();
        //2.计时器启动
        stopWatch.start();
        //Spring上下文
        ConfigurableApplicationContext context = null;
        //异常报告列表
        Collection exceptionReporters = new ArrayList();
        //3.设置系统配置java.awt.headless
        this.configureHeadlessProperty();
        //4.获取所有启动监听器
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        //5.启动监听器
        listeners.starting();
        // 异常通知者列表
        Collection exceptionReporters;
        try {
            //6.初始化应用启动参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //7.准备环境变量
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            //8.设置spring.beaninfo.ignore配置
            this.configureIgnoreBeanInfo(environment);
            //9.打印Banner
            Banner printedBanner = this.printBanner(environment);
            //10.创建上下文
            context = this.createApplicationContext();
            //11.获取异常报告者列表
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            //12.准备上下文对象
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            //13.刷新上下文对象
            this.refreshContext(context);
            //14.刷新上下文对象后续工作
            this.afterRefresh(context, applicationArguments);
            //15.停止计时
            stopWatch.stop();
            //16.打印启动信息
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
            // 17.执行启动监听器started方法
            listeners.started(context);
            //18.执行ApplicationRunner和CommandLineRunner
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            // 处理运行异常
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            //19.执行启动监听器running方法
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            // 处理运行异常
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
}
    
    
StopWatch
Simple stop watch, allowing for timing of a number of tasks,
exposing total running time and running time for each named task.

上一篇     下一篇
ServiceLoader机制及实例

什么是smart beta

java初始化顺序

常用负载均衡算法

注解组合和继承

spring-boot-starter-parent的作用