springboot源码—-启动概述

  • Post author:
  • Post category:其他


启动类代码开始阅读:SpringApplication.run(SpringbootSimpleApplication.class, args);

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

初始化操作

@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	this.resourceLoader = resourceLoader;
	Assert.notNull(primarySources, "PrimarySources must not be null");
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	this.webApplicationType = WebApplicationType.deduceFromClasspath();
    //从spring.factory文件中读取对应的className,并且实例化
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); 
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
	this.mainApplicationClass = deduceMainApplicationClass();
}

查看run方法

public ConfigurableApplicationContext run(String... args) {
	StopWatch stopWatch = new StopWatch();//计时器
	stopWatch.start();
	ConfigurableApplicationContext context = null;    //上下文
	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
	configureHeadlessProperty();    //设置一些java.awt.headless等缺少显示器、键盘等配置
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting();    //向listeners广播启动事件
	try {
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//封装启动参数
        //创建envirenment
		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        //配置忽略bean信息
		configureIgnoreBeanInfo(environment);
		Banner printedBanner = printBanner(environment);
        //创建上下文AnnotationConfigServletWebServerApplicationContext
        //注册所有跟注解相关的后置处理器,到BeanDefinitionRegistry
		context = createApplicationContext();
		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
			new Class[] { ConfigurableApplicationContext.class }, context);
		//prepareContext是SpringApplication的private方法
        //把environment导入ApplicationContext;
        //Initializers里加载ApplicationContext;
        //通知initializers,ApplicationContext已创建,但尚未加载source
        //往BeanFactory添加ApplicationArguments和Banner两个单例对象
        //通过BeanDefinitionLoader往BeanFactory添加source的BeanDefinition
        //通知listeners,ApplicationContext已经加载source,但尚未refresh
        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;
}



版权声明:本文为m1993zhao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。