todo sourcecode

本文深入剖析了SpringApplication的启动流程,从run方法开始,逐步解析了初始化、环境准备、上下文创建、刷新、监听器处理等核心步骤。通过详细的代码注释,揭示了Spring Boot应用启动背后的机制,帮助读者理解Spring Boot的运行原理。
摘要由CSDN通过智能技术生成
SpringApplication.run(DemoApplication.class, args); -> 
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class[]{primarySource}, args); // ->
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return (new SpringApplication(primarySources)).run(args); //初始化SpringApplication,并运行 ->
}

//初始化SpringApplication

public SpringApplication(Class<?>... primarySources) {
    this((ResourceLoader)null, primarySources); ->//设置resourceLoader为null, primarySources 为启动类DemoApplication.class
}
//todo 标明每个属性的作用
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; //设置java.swt.headless的值
    this.registerShutdownHook = true;
    this.additionalProfiles = Collections.emptySet();
    this.isCustomEnvironment = false;
    this.lazyInitialization = false;
    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));
    this.webApplicationType = WebApplicationType.deduceFromClasspath(); //设置类类型
    this.bootstrapRegistryInitializers = this.getBootstrapRegistryInitializersFromSpringFactories();
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();//根据堆栈记录获取运行类
}
//运行初始化的类
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch(); //获取每个任务的运行时间状况
    stopWatch.start(); //设置当前任务名称和启动时间
    DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();//加载启动上下文 这里第一次执行,注册表初始器为空 
    ConfigurableApplicationContext context = null; //配置应用程序上下文,还没有实现
    this.configureHeadlessProperty(); //设置java.awt.headless ,默认为true. java.awt.headless是J2SE的一种模式用于在缺少显示屏、键盘或者鼠标时的系统配置,很多监控工具如jconsole 需要将该值设置为true.
        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); //根据环境配置属性:spring.beaninfo.ignore
        Banner printedBanner = this.printBanner(environment); //打印banner信息
        context = this.createApplicationContext(); //创建应用程序上下文 ->
        context.setApplicationStartup(this.applicationStartup); //为应用程序上下文设置应用程序启动
        this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner); //准备上下文 ->
        this.refreshContext(context); //刷新上下文 ->
        this.afterRefresh(context, applicationArguments); //刷新之后  默认为空方法,可拓展
        stopWatch.stop(); //关闭运行监听器
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context); //监听器启动应用程序上下文
        this.callRunners(context, applicationArguments);//运行器运行程序
    } catch (Throwable var10) {
        this.handleRunFailure(context, var10, listeners);
        throw new IllegalStateException(var10);
    }

    try {
        listeners.running(context); //监听器们运行应用程序上下文
        return context;
    } catch (Throwable var9) {
        this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
        throw new IllegalStateException(var9);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值