springboot源码理解八、run方法执行过程(刷新应用上下文)

run方法执行过程(刷新应用上下文)

  • SpringApplication.run
    • 刷新应用上下文
      • 调用spring的refresh()方法
        • invokeBeanFactoryPostProcessors(beanFactory)
          • 解析被@Configuration注解修饰的配置类
            • 处理@ComponentScan注解
            • 处理@Import注解

springboot版本:2.2.9.RELEASE。

SpringApplication.run

run方法是springboot启动过程中非常重要的步骤。

SpringBootMytestApplication#main →
SpringApplication#run(java.lang.Class, java.lang.String...) → SpringApplication#run(java.lang.Class[], java.lang.String[]) →
SpringApplication#run(java.lang.String…)

SpringApplication#run(java.lang.String…)

/**
 * 运行spring引用,创建并刷新一个ApplicationContext。
 * Run the Spring application, creating and refreshing a new
 * {@link ApplicationContext}.
 * @param args the application arguments (usually passed from a Java main method)
 * @return a running {@link ApplicationContext}
 */
public ConfigurableApplicationContext run(String... args) {
	// 记录执行时间
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	// ConfigurableApplicationContext应用上下文,Spring IoC容器的一种实现。
	ConfigurableApplicationContext context = null;
	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
	configureHeadlessProperty();
	// 1、获取启动监听器
	SpringApplicationRunListeners listeners = getRunListeners(args);
	listeners.starting();
	try {
		///2、构造上下文环境
		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
		configureIgnoreBeanInfo(environment);
		Banner printedBanner = printBanner(environment);
		///3、初始化应用上下文
		context = createApplicationContext();
		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
				new Class[] { ConfigurableApplicationContext.class }, context);
		// 4、刷新上下文的准备阶段
		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
		// 5、刷新上下文
		refreshContext(context);
		// 6、刷新上下文后的扩展
		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;
}

刷新应用上下文

SpringApplication#refreshContext

SpringApplication#refresh

AbstractApplicationContext#refresh

调用spring的refresh()方法

AbstractApplicationContext#refresh
refresh方法,这个方法其实标志着IoC容器初始化过程的正式启动。
在Spring IoC容器初始化过程简单说过。

@Override
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// Prepare this context for refreshing.
		prepareRefresh();

		// Tell the subclass to refresh the internal bean factory.
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

		// Prepare the bean factory for use in this context.
		prepareBeanFactory(beanFactory);

		try {
			// Allows post-processing of the bean factory in context subclasses.
			postProcessBeanFactory(beanFactory);

			// Invoke factory processors registered as beans in the context.
			invokeBeanFactoryPostProcessors(beanFactory);

			// Register bean processors that intercept bean creation.
			registerBeanPostProcessors(beanFactory);

			// Initialize message source for this context.
			initMessageSource();

			// Initialize event multicaster for this context.
			initApplicationEventMulticaster();

			// Initialize other special beans in specific context subclasses.
			onRefresh();

			// Check for listener beans and register them.
			registerListeners();

			// Instantiate all remaining (non-lazy-init) singletons.
			finishBeanFactoryInitialization(beanFactory);

			// Last step: publish corresponding event.
			finishRefresh();
		}

		catch (BeansException ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Exception encountered during context initialization - " +
						"cancelling refresh attempt: " + ex);
			}

			// Destroy already created singletons to avoid dangling resources.
			destroyBeans();

			// Reset 'active' flag.
			cancelRefresh(ex);

			// Propagate exception to caller.
			throw ex;
		}

		finally {
			// Reset common introspection caches in Spring's core, since we
			// might not ever need metadata for singleton beans anymore...
			resetCommonCaches();
		}
	}
}

invokeBeanFactoryPostProcessors(beanFactory)

AbstractApplicationContext#invokeBeanFactoryPostProcessors
springboot源码理解八、run方法执行过程(刷新应用上下文)_第1张图片

PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory, java.util.List)
springboot源码理解八、run方法执行过程(刷新应用上下文)_第2张图片
跳进ConfigurationClassPostProcessor的实现
springboot源码理解八、run方法执行过程(刷新应用上下文)_第3张图片
ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry
springboot源码理解八、run方法执行过程(刷新应用上下文)_第4张图片
ConfigurationClassPostProcessor#processConfigBeanDefinitions
springboot源码理解八、run方法执行过程(刷新应用上下文)_第5张图片
parser.parse(candidates)是我们重点要关注的方法。

解析被@Configuration注解修饰的配置类

parser.parse(candidates)

// Parse each @Configuration class,解析被@Configuration注解修饰的配置类。

ConfigurationClassParser#parse(java.util.Set)
springboot源码理解八、run方法执行过程(刷新应用上下文)_第6张图片
ConfigurationClassParser#parse(org.springframework.core.type.AnnotationMetadata, java.lang.String)

org.springframework.context.annotation.ConfigurationClassParser#processConfigurationClass

org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass

处理@ComponentScan注解

org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass中
springboot源码理解八、run方法执行过程(刷新应用上下文)_第7张图片
处理@ComponentScan注解,这个sourceClass是springboot启动类。
允许多个@ComponentScan。

注意这一行this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());

org.springframework.context.annotation.ComponentScanAnnotationParser#parse
springboot源码理解八、run方法执行过程(刷新应用上下文)_第8张图片
因为我们没有配置@ComponentScan的basePackages,
会取ClassUtils.getPackageName(clazz)作为basePackages;

有了basePackages之后,接下来执行scanner.doScan()方法,
在这里插入图片描述
org.springframework.context.annotation.ClassPathBeanDefinitionScanner#doScan
springboot源码理解八、run方法执行过程(刷新应用上下文)_第9张图片
ClassPathScanningCandidateComponentProvider#findCandidateComponents

找basePackages下的Components
springboot源码理解八、run方法执行过程(刷新应用上下文)_第10张图片
ClassPathScanningCandidateComponentProvider#scanCandidateComponents

由basePackages拼接packageSearchPath = classpath*:com/duohoob/springbootmytest/**/*.class
扫描到com.duohoob.springbootmytest.controller.TestController
封装为ScannedGenericBeanDefinition。
springboot源码理解八、run方法执行过程(刷新应用上下文)_第11张图片
我们再回到ClassPathBeanDefinitionScanner#doScan
springboot源码理解八、run方法执行过程(刷新应用上下文)_第12张图片
BeanDefinitionReaderUtils#registerBeanDefinition
springboot源码理解八、run方法执行过程(刷新应用上下文)_第13张图片
这一步是将BeanDefinition注册到Spring注册到容器。

处理@Import注解

org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass中
在这里插入图片描述
getImports(sourceClass),获取通过@Import注解导入的组件。

再回到ConfigurationClassParser#parse(java.util.Set)
springboot源码理解八、run方法执行过程(刷新应用上下文)_第14张图片
ConfigurationClassParser.DeferredImportSelectorHandler#process

这是springboot自动配置第三方依赖中bean的入口。
springboot源码理解八、run方法执行过程(刷新应用上下文)_第15张图片
org.springframework.context.annotation.ConfigurationClassParser.DeferredImportSelectorGroupingHandler#processGroupImports

这是获取META-INF\spring.factories中的配置类。
springboot源码理解八、run方法执行过程(刷新应用上下文)_第16张图片
再回到ConfigurationClassPostProcessor#processConfigBeanDefinitions
springboot源码理解八、run方法执行过程(刷新应用上下文)_第17张图片
this.reader.loadBeanDefinitions(configClasses);
这一步是把META-INF\spring.factories中配置类的@Bean加载到容器中。

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)