springBoot总结

springBoot总结

springBoot启动流程

入口:
SpringApplication.run(SpringApplication.class, args);
走进run看一下他的调用过程,走入到核心代码
首先会走构造方法初始化各种参数,

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;
        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));
        ///获取Web 应用程序类型
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        this.bootstrappers = new ArrayList(this.getSpringFactoriesInstances(Bootstrapper.class));
        // 设置应用上下文初始化器
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        // 设置应用监听器
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        // 推导出当前启动的main方法所在的类
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

然后继续跟着run走,直接找到核心方法

public ConfigurableApplicationContext run(String... args) {
		//创建计时器并启动
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        //初始化应用上下文
        ConfigurableApplicationContext context = null;
        //初始化异常报告集合
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        //设置系统属性“java.awt.headless”的值,默认为true
        this.configureHeadlessProperty();
        //获取到所有的listeners,之后执行listeners的不同方法,实现不同阶段的扩展
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
        	//初始化默认应用参数类
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            //根据监听器和应用参数来准备spring环境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            //将要忽略的bean的参数打开
            this.configureIgnoreBeanInfo(environment);
            //创建banner打印类
            Banner printedBanner = this.printBanner(environment);
            //创建应用上下文
            context = this.createApplicationContext();
            //准备异常报告器,用来支持报告关于启动的错误
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            //准备应用上下文
            this.prepareContext(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, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }
        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }
prepareContext

准备应用上下文,这里的主要工作包括填充环境信息,后置处理添加conversionService,ApplicationContextInitializer扩展, 加载资源等等,详细的可以参考https://blog.csdn.net/m0_37607945/article/details/106447591
在这里插入图片描述

refreshContext

这个方法中加载beanFactory,后置处理器等等,可以参考本人另一篇博客https://blog.csdn.net/jingju8354/article/details/125053731
在这里插入图片描述

afterContext

这个方法本身没有实现,Spring提供了ApplicationContext加载完成后的回调接口。我们可以实现ApplicationRunner,CommandLineRunner接口去自定义处理我们的内容。
在这里插入图片描述

------------------------如有错误请及时提醒作者,感谢!!-----------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值