SpringBoot源码解析 SpringApplication.run基本流程

、、springboot启动流程—主类上

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

然后其调用了下面这个
primarySources是传入的主类

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

new SpringApplication(primarySources)

主要是先填入部分的属性

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		//资源加载器传入的是null
       this.resourceLoader = resourceLoader;
        //非空判定
		Assert.notNull(primarySources, "PrimarySources must not be null");
        //主类封装为LinkedHashSet
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        //从类路径中推测web应用类型,这个主要是看存在那个类,一般是Servlet
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
        //使用SpringFactoriesLoader.loadFactoryNames加载
        //ApplicationContextInitializer.class  ApplicationListener.class并设置
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		//是通过获取当前的方法栈new RuntimeException().getStackTrace();
        //然后找到使用main方法的那一个类,然后返回该类
        this.mainApplicationClass = deduceMainApplicationClass();
	}

run(args);

public ConfigurableApplicationContext run(String... args) {
       //记录事件的对象
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
       //ApplicationContext的子接口,上下文
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
        //获取监听器
		SpringApplicationRunListeners listeners = getRunListeners(args);
        //启动监听器 
       //就是this.initialMulticaster.multicastEvent(new ApplicationStartingEvent
		listeners.starting();
		try {
            //封装保存args
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			//构造应用上下文环境
            ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
		    //处理需要忽略的bean
            configureIgnoreBeanInfo(environment);
            //打印banner
			Banner printedBanner = printBanner(environment);
            //初始化应用上下文,IOC容器,也就是DefaultListableBeanFactory会在这时候创建并添加入applicationcontext
			context = createApplicationContext();
            //SpringBootExceptionReporter的实例获取(用于支持springboot启动错误)
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			
            //刷新上下文之前的准备工作
            prepareContext(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, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值