【细读Spring Boot源码】重中之重refresh()

  • Post author:
  • Post category:其他




前言

版本:spring-boot-2.7.3 | spring-context-5.3.22


在Spring Boot启动过程中【细读Spring Boot源码】启动步骤

主流程详情7中

applicationContext.refresh();

这个操作是加载或刷新容器,把所有的配置转换成响应的对象并存入容器。

下面看下他的具体执行流程



调用主流程

主流程使用了模板模式是一个模板方法

@Override
public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// 步骤记录器记录开始
		StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");

		// Prepare this context for refreshing.
		// 准备刷新。详看1	
		prepareRefresh();

		// Tell the subclass to refresh the internal bean factory.
		// 告诉子类刷新内部 bean 工厂,得到Bean工厂。详看2
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

		// Prepare the bean factory for use in this context.
		//  准备 bean 工厂以在此上下文中使用。详看3
		prepareBeanFactory(beanFactory);

		try {
			// Allows post-processing of the bean factory in context subclasses.
			// 在标准初始化后修改应用程序上下文的内部bean工厂。
			// 所有bean定义都已加载,但尚未实例化bean。
			// 这允许在某些应用程序上下文实现中注册特殊的BeanPostProcessors等。详看4
			postProcessBeanFactory(beanFactory);

			StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
			// Invoke factory processors registered as beans in the context.
			// 调用工厂处理注册Bean到上下文
			invokeBeanFactoryPostProcessors(beanFactory);

			// Register bean processors that intercept bean creation.
			// 从BeanFactory扫描找出BeanPostProcessor,实例化并注册到BeanFactory里beanPostProcessors属性中
			// 详看6
			registerBeanPostProcessors(beanFactory);
			beanPostProcess.end();

			// Initialize message source for this context.
			// 初始化消息资源。详看7
			initMessageSource();

			// Initialize event multicaster for this context.
			// 初始化应用事件多播器。详看8
			initApplicationEventMulticaster();

			// Initialize other special beans in specific context subclasses.
			// 在特定上下文子类中初始化其他特殊bean。详看9
			onRefresh();

			// Check for listener beans and register them.
			// 注册监听器。详看10
			registerListeners();

			// Instantiate all remaining (non-lazy-init) singletons.
			// 实例化所有剩余的 (非惰性init) 单例。详看11
			finishBeanFactoryInitialization(beanFactory);

			// Last step: publish corresponding event.
			// 发布相应的事件。详看12
			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();
			// 刷新结束
			contextRefresh.end();
		}
	}
}



详情1——准备刷新prepareRefresh()

Springboot容器没有直接使用AbstractApplicationContext抽象类默认实现方法,Springboot容器是AnnotationConfigServletWebServerApplicationContext类,

功能如下:

  1. 清除之前容器缓存
  2. 记录时间,撤销关闭状态,开启活跃状态
  3. 初始化属性源信息(Property)
  4. 验证环境信息里一些必须存在的属性
  5. 把早期使用的监听器存入监听器列表

实现如下

protected void prepareRefresh() {
	// 清除缓存,理解这里在清除之前容器的缓存。例如:nacos引导容器启动时使用的缓存
	this.scanner.clearCache();
	// 再调用AbstractApplicationContext抽象类默认实现
	super.prepareRefresh();
}

看下AbstractApplicationContext抽象类默认实现。

protected void prepareRefresh() {
	// Switch to active.
	// 切换到活跃
	this.startupDate = System.currentTimeMillis();
	this.closed.set(false);
	this.active.set(true);

	if (logger.isDebugEnabled()) {
		if (logger.isTraceEnabled()) {
			logger.trace("Refreshing " + this);
		}
		else {
			logger.debug("Refreshing " + getDisplayName());
		}
	}

	// Initialize any placeholder property sources in the context environment.
	// 初始化上下文环境中的任何占位符属性源
	initPropertySources();

	// Validate that all properties marked as required are resolvable:
	// see ConfigurablePropertyResolver#setRequiredProperties
	// 验证标记为必需的所有属性都是可解析的
	getEnvironment().validateRequiredProperties();

	// Store pre-refresh ApplicationListeners...
	// 存储预刷新 ApplicationListeners
	if (this.earlyApplicationListeners == null) {
		this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
	}
	else {
		// Reset local application listeners to pre-refresh state.
		// 清空
		this.applicationListeners.clear();
		// 把早期使用的监听器存入监听器列表
		this.applicationListeners.addAll(this.earlyApplicationListeners);
	}

	// Allow for the collection of early ApplicationEvents,
	// to be published once the multicaster is available...
	// 允许早期应用程序事件的集合,一旦multicaster可用就发布
	this.earlyApplicationEvents = new LinkedHashSet<>();
}



详看2——obtainFreshBeanFactory()

获取BeanFactory

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
	// 刷新Bean工厂
	refreshBeanFactory();
	// 得到Bean工厂
	return getBeanFactory();
}

其中

refreshBeanFactory()

是AbstractApplicationContext抽象类的一个抽象方法。

子类必须实现此方法才能执行实际的配置负载。在任何其他初始化工作之前,该方法都会通过refresh() 调用。

子类将创建一个新的bean工厂并持有对它的引用,或者返回它持有的单个BeanFactory实例。在后一种情况下,如果多次刷新上下文,通常会抛出IllegalStateException。

实际上就是:刷新内部 bean 工厂,把当前上下文的Bean工厂切换为当前的Bean工厂。

protected final void refreshBeanFactory() throws IllegalStateException {
	// 如果当前值 = 期望值,则原子地将值设置为给定的更新值
	// 这个来判断当前容器是否刷新过。即refreshed的值为false,就把值替换为true,并返回true。取反
	if (!this.refreshed.compareAndSet(false, true)) {
		// 如果刷新过就抛出异常
		throw new IllegalStateException(
				"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
	}
	// 替换。为序列化目的指定一个id,如果需要的话,允许这个BeanFactory从这个id反序列化回BeanFactory对象。
	this.beanFactory.setSerializationId(getId());
}



详看3——prepareBeanFactory(beanFactory)

设置这个工厂(BeanFactory)的标准上下文特征(一些配置),例如上下文的类加载器和后处理器。

在这之前先了解几个方法:


  • void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

    这个方法:添加一个新的BeanPostProcessor,该处理器将应用于该工厂创建的bean。在工厂配置期间调用。

注意: 此处提交的后处理器将按注册顺序应用; 通过实现org.springframework.core.Ordered接口表达的任何排序语义将被忽略。

请注意,自动检测的后处理器 (例如,在ApplicationContext中作为bean) 将始终在以编程方式注册的处理器之后应用。


  • void ignoreDependencyInterface(Class<?> ifc);

    这个方法:忽略给定的依赖接口进行自动查询。

    这通常将由应用程序上下文用于注册依赖关系,这些依赖关系以其他方式解决,例如通过BeanFactoryAware的BeanFactory或通过ApplicationContext的ApplicationContext。

    默认情况下,仅忽略BeanFactoryAware接口。对于要忽略的其他类型,请为每种类型调用此方法。


  • void registerResolvableDependency(Class<?> dependencyType, @Nullable Object autowiredValue);

    这个方法:用相应的autowired值注册一个特殊的依赖类型。这适用于factorycontext引用,这些引用应该是自动的,但在工厂中未定义为bean: 例如,ApplicationContext类型的依赖关系解析为bean所在的ApplicationContext实例。

注意: 在普通BeanFactory中没有注册这样的默认类型,甚至对于BeanFactory接口本身也没有。


Spring源码笔记之beanFactory.registerResolvableDependency()方法

,这个文章里解释的很清楚,还有使用示例。


  • void registerSingleton(String beanName, Object singletonObject);

    这个方法:在给定的bean名称下,在bean注册表中将给定的现有对象注册为singleton。

    给定的实例应该是完全初始化的; 注册表不会执行任何初始化回调 (特别是,它不会调用InitializingBean的afterPropertiesSet方法)。给定的实例也不会收到任何破坏回调 (如dispoablebean的destroy方法)。

    在full BeanFactory中运行时: 如果您的bean应该接收初始化和/或销毁回调,请注册bean定义而不是现有实例。

    通常在注册表配置期间调用,但也可以用于单例的运行时注册。因此,注册表实现应该同步单例访问; 如果它支持BeanFactory的单例延迟初始化,则无论如何都必须这样做

根据上面的铺垫,看下面就很顺畅。分为3部分

  1. 设置表达式解析器
  2. 添加应用上下文Aware的BeanPostProcessor和忽略EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware、ApplicationStartupAware
  3. 设置特殊的类型对应的bean。BeanFactory对应刚刚获取的BeanFactory;ResourceLoader、ApplicationEventPublisher、ApplicationContext这3个接口对应的bean都设置为当前的Spring容器
  4. 注入一些其它信息的bean,比如environment、systemProperties等
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	// Tell the internal bean factory to use the context's class loader etc.
	// 将内部bean工厂使用上下文的类加载器等
	beanFactory.setBeanClassLoader(getClassLoader());
	// 忽略表达式
	if (!shouldIgnoreSpel) {
		// 设置Bean表达式解析器
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
	}
	// 添加属性编辑器注册器
	beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

	// Configure the bean factory with context callbacks.
	// 添加上下文感知处理器
	beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
	// 忽略环境感知
	beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
	// 忽略内含价值解析器感知
	beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
	// 忽略资源加载感知
	beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
	// 忽略应用程序事件发布者感知
	beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
	// 忽略消息资源感知
	beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
	// 忽略应用上下文感知
	beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
	// 忽略应用启动步骤感知
	beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);

	// BeanFactory interface not registered as resolvable type in a plain factory.
	// MessageSource registered (and found for autowiring) as a bean.
	// 其他类想要用BeanFactory.class,就注入beanFactory。下面几个同理
	beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
	beanFactory.registerResolvableDependency(ResourceLoader.class, this);
	beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
	beanFactory.registerResolvableDependency(ApplicationContext.class, this);

	// Register early post-processor for detecting inner beans as ApplicationListeners.
	// 注册早期的后处理器,用于检测内部bean作为应用程序侦听器。
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

	// Detect a LoadTimeWeaver and prepare for weaving, if found.
	if (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
		beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
		// Set a temporary ClassLoader for type matching.
		beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
	}

	// Register default environment beans.
	// 注入一些其它信息的bean,比如environment、systemProperties等
	if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
		beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
	}
	if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
		beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
	}
	if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
		beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
	}
	if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
		beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
	}
}



详看4——postProcessBeanFactory(beanFactory);

这步处理所有的Bean

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	super.postProcessBeanFactory(beanFactory);
	if (this.basePackages != null && this.basePackages.length > 0) {
		this.scanner.scan(this.basePackages);
	}
	if (!this.annotatedClasses.isEmpty()) {
		this.reader.register(ClassUtils.toClassArray(this.annotatedClasses));
	}
}

代码

super.postProcessBeanFactory(beanFactory)

详情

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	// 添加一个BeanPostProcessor,
	// 用于初始化ServletContextAware、ServletConfigAware时使用,即在初始化ServletContext或ServletConfig之前使用
	beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));
	// 忽略给定的ServletContextAware依赖接口进行自动查询。
	// 默认仅忽略BeanFactoryAware接口。对于要忽略的其他类型,请为每种类型调用此方法
	beanFactory.ignoreDependencyInterface(ServletContextAware.class);
	// 注册Web应用程序范围,request范围和session范围
	registerWebApplicationScopes();
}

代码

registerWebApplicationScopes();

详情

private void registerWebApplicationScopes() {
	// 这个是内部类,里面处理从beanFactory里取request、session对应的话就把域包装到set里封装成这个对象
	ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(getBeanFactory());
	// 使用指定的beanFactory注册web具体request、session、globalSession范围
	WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());
	// 把上面的域恢复注册到beanFactory里,以确保后面对域的正确使用。
	// 上面方法内注册了一个新的request、session、application,这里如果之前有那么就恢复之前的
	existingScopes.restore();
}

代码

WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory());

详情

public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory) {
	registerWebApplicationScopes(beanFactory, null);
}
	
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
		@Nullable ServletContext sc) {

	// 注册一个新的request域
	beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
	// 注册一个新的session域
	beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
	// 如果服务上下文不为空,
	if (sc != null) {
		ServletContextScope appScope = new ServletContextScope(sc);
		// 注册一个application域
		beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// 并注册为ServletContext属性,以供ContextCleanupListener检测。
		sc.setAttribute(ServletContextScope.class.getName(), appScope);
	}

	// 为request、response、session、webRequest注册解析依赖
	beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
	beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
	beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
	beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
	if (jsfPresent) {
		FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
	}
}



详看5——invokeBeanFactoryPostProcessors(beanFactory);

调用BeanFactory后置处理,把所有的Bean注册到beanFactory。


invokeBeanFactoryPostProcessors

是一个方法。用于调用 BeanFactoryPostProcessor 接口实现类的方法。它的作用是在 Spring 容器初始化过程中,在 BeanDefinition 加载完成、实例化之前,给予开发者一个机会去修改或者扩展已注册的 BeanDefinition。

需要了解接口BeanPostProcessor,看这篇文章


【细读Spring Boot源码】Spring扩展点接口BeanPostProcessor-储备

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	// 调用一个静态处理方法
	PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

	// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
	// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
	if (!NativeDetector.inNativeImage() && beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
		beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
		beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
	}
}

源码中有一段WARNING,译文如下:

尽管看起来这个方法的主体很容易被重构,以避免使用多个循环和多个列表,但使用多个列表和多次传递处理器的名称是有意的。我们必须确保我们遵守 PriorityOrdered 和 Ordered 处理器的契约。具体来说,我们不能导致处理器被实例化(通过getBean()调用)或以错误的顺序在 ApplicationContext 中注册。在提交改变此方法的拉动请求(PR)之前,请查看所有涉及改变PostProcessorRegistrationDelegate的被拒绝的PR列表,以确保你的建议不会导致破坏性的改变:

https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

梳理一下下面大段代码的逻辑,整体分为2部分。

第一部分:这里把早期添加到context里的BeanFactoryPostProcessor执行分类、排序、invoke

这里是处理Bean定义注册的逻辑

if (beanFactory instanceof BeanDefinitionRegistry) {
	...
} else {
	...
}

第二部分:这里是后半部分,从中取出BeanDefinitionRegistryPostProcessor类的所有实现,然后分类、排序、invoke

public static void invokeBeanFactoryPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

	// 翻译在上面
	// WARNING: Although it may appear that the body of this method can be easily
	// refactored to avoid the use of multiple loops and multiple lists, the use
	// of multiple lists and multiple passes over the names of processors is
	// intentional. We must ensure that we honor the contracts for PriorityOrdered
	// and Ordered processors. Specifically, we must NOT cause processors to be
	// instantiated (via getBean() invocations) or registered in the ApplicationContext
	// in the wrong order.
	//
	// Before submitting a pull request (PR) to change this method, please review the
	// list of all declined PRs involving changes to PostProcessorRegistrationDelegate
	// to ensure that your proposal does not result in a breaking change:
	// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
	// 首先调用BeanDefinitionRegistryPostProcessors,如果存在
	Set<String> processedBeans = new HashSet<>();

	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
		
		// 循环处理出入的beanFactoryPostProcessors
		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				BeanDefinitionRegistryPostProcessor registryProcessor =
						(BeanDefinitionRegistryPostProcessor) postProcessor;
				// 如果是BeanDefinitionRegistryPostProcessor调用这个扩展钩子
				registryProcessor.postProcessBeanDefinitionRegistry(registry);
				// 处理完添加到常规的PostProcessors
				registryProcessors.add(registryProcessor);
			}
			else {
				// 添加到常规的PostProcessors
				regularPostProcessors.add(postProcessor);
			}
		}

		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let the bean factory post-processors apply to them!
		// Separate between BeanDefinitionRegistryPostProcessors that implement
		// PriorityOrdered, Ordered, and the rest.
		List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

		// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
		// 得到所有BeanDefinitionRegistryPostProcessor类型的Bean名称
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		// 按类型分类,处理PriorityOrdered类型
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		// 排序
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		// 添加需要注册的列表
		registryProcessors.addAll(currentRegistryProcessors);
		// 注册,调用Bean定义注册表后处理器
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
		currentRegistryProcessors.clear();

		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
		postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		// 按类型分类,处理Ordered类型
		for (String ppName : postProcessorNames) {
			if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		registryProcessors.addAll(currentRegistryProcessors);
		// 注册
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
		currentRegistryProcessors.clear();

		// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
		// 最后注册处理其他类型BeanDefinitionRegistryPostProcessor
		boolean reiterate = true;
		while (reiterate) {
			reiterate = false;
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (!processedBeans.contains(ppName)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
					reiterate = true;
				}
			}
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry, beanFactory.getApplicationStartup());
			currentRegistryProcessors.clear();
		}

		// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
		// 现在,调用到目前为止处理的所有处理器的postProcessBeanFactory回调。
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}

	else {
		// Invoke factory processors registered with the context instance.
		// 直接调用处理的所有处理器的postProcessBeanFactory回调。
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}

	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let the bean factory post-processors apply to them!、
	// 获取所有BeanFactoryPostProcessor
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

	// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	// 分类
	List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
	List<String> orderedPostProcessorNames = new ArrayList<>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
	for (String ppName : postProcessorNames) {
		if (processedBeans.contains(ppName)) {
			// skip - already processed in first phase above
		}
		else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// 先处理priorityOrdered的PostProcessor
	// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	// 处理调用Ordered的PostProcessor
	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
	for (String postProcessorName : orderedPostProcessorNames) {
		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

	// Finally, invoke all other BeanFactoryPostProcessors.
	// 最后调用剩余的BeanFactoryPostProcessors
	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
	for (String postProcessorName : nonOrderedPostProcessorNames) {
		nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

	// Clear cached merged bean definitions since the post-processors might have
	// modified the original metadata, e.g. replacing placeholders in values...
	beanFactory.clearMetadataCache();
}


BeanFactoryPostProcessor

是在 Spring 容器实例化 Bean 之前调用的接口。

它允许您对应用程序上下文的 BeanFactory 进行自定义修改。


BeanFactoryPostProcessor

的实现类可以修改 Bean 的定义,添加额外的属性或修改属性值。

它通常用于在应用程序上下文准备就绪之前对 Bean 定义进行修改,例如更改 Bean 的作用域、添加属性占位符解析器等。


BeanFactoryPostProcessor

在容器启动时执行,只对配置的 BeanFactory 生效。



详看6——registerBeanPostProcessors(beanFactory);

发现注册BeanPostProcessor

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	// 这里使用了一个静态方法
	PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

看下registerBeanPostProcessors

源码中有一段WARNING,译文如下:

尽管看起来这个方法的主体很容易被重构,以避免使用多个循环和多个列表,但使用多个列表和多次传递处理器的名称是有意的。我们必须确保我们遵守 PriorityOrdered 和 Ordered 处理器的契约。具体来说,我们不能导致处理器被实例化(通过getBean()调用)或以错误的顺序在 ApplicationContext 中注册。在提交改变此方法的拉动请求(PR)之前,请查看所有涉及改变PostProcessorRegistrationDelegate的被拒绝的PR列表,以确保你的建议不会导致破坏性的改变:

https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

public static void registerBeanPostProcessors(
		ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
	
	// 这段翻译看上面
	// WARNING: Although it may appear that the body of this method can be easily
	// refactored to avoid the use of multiple loops and multiple lists, the use
	// of multiple lists and multiple passes over the names of processors is
	// intentional. We must ensure that we honor the contracts for PriorityOrdered
	// and Ordered processors. Specifically, we must NOT cause processors to be
	// instantiated (via getBean() invocations) or registered in the ApplicationContext
	// in the wrong order.
	//
	// Before submitting a pull request (PR) to change this method, please review the
	// list of all declined PRs involving changes to PostProcessorRegistrationDelegate
	// to ensure that your proposal does not result in a breaking change:
	// https://github.com/spring-projects/spring-framework/issues?q=PostProcessorRegistrationDelegate+is%3Aclosed+label%3A%22status%3A+declined%22

	// 取到所有BeanPostProcessor类型的Bean名称
	String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

	// Register BeanPostProcessorChecker that logs an info message when
	// a bean is created during BeanPostProcessor instantiation, i.e. when
	// a bean is not eligible for getting processed by all BeanPostProcessors.
	// 重新计算BeanPostProcessor的数量
	int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
	// 添加
	beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

	// Separate between BeanPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	// 根据不同的排序方式进行分类
	List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
	List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
	List<String> orderedPostProcessorNames = new ArrayList<>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
	for (String ppName : postProcessorNames) {
		if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			priorityOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}

	// First, register the BeanPostProcessors that implement PriorityOrdered.
	// 注册PriorityOrdered类型的BeanPostProcessors
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

	// Next, register the BeanPostProcessors that implement Ordered.
	// 注册Ordered类型的BeanPostProcessors
	List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
	for (String ppName : orderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		orderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	sortPostProcessors(orderedPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, orderedPostProcessors);

	// Now, register all regular BeanPostProcessors.
	// 注册所有常规的BeanPostProcessors
	List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
	for (String ppName : nonOrderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		nonOrderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

	// Finally, re-register all internal BeanPostProcessors.
	// 最后,再注册所有的内部BeanPostProcessors
	sortPostProcessors(internalPostProcessors, beanFactory);
	registerBeanPostProcessors(beanFactory, internalPostProcessors);

	// Re-register post-processor for detecting inner beans as ApplicationListeners,
	// moving it to the end of the processor chain (for picking up proxies etc).
	// 重新注册后处理器,以检测内部bean作为应用程序侦听器,将其移动到处理器链的末端 (用于拾取代理等)
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

看下实际的注册逻辑,实际上还是调用了

factory.addBeanPostProcessor()方法

跟上面Spring的原理一样。

private static void registerBeanPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) {

	if (beanFactory instanceof AbstractBeanFactory) {
		// Bulk addition is more efficient against our CopyOnWriteArrayList there
		((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors);
	}
	else {
		for (BeanPostProcessor postProcessor : postProcessors) {
			beanFactory.addBeanPostProcessor(postProcessor);
		}
	}
}

从原来的位置移除,在重新添加


this.beanPostProcessors

是BeanPostProcessorCacheAwareList的实例。

BeanPostProcessorCacheAwareList继承了

CopyOnWriteArrayList

public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
	Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
	// Remove from old position, if any
	this.beanPostProcessors.remove(beanPostProcessor);
	// Add to end of list
	this.beanPostProcessors.add(beanPostProcessor);
}



详看7——initMessageSource();

初始化消息源(MessageSource),以支持国际化和本地化的消息处理

protected void initMessageSource() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	// beanFactory中包好messageSource的Bean
	if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
		// 取到
		this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
		// 让消息来源知道父消息来源
		if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
			HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
			if (hms.getParentMessageSource() == null) {
				// Only set parent context as parent MessageSource if no parent MessageSource
				// registered already.
				hms.setParentMessageSource(getInternalParentMessageSource());
			}
		}
		if (logger.isTraceEnabled()) {
			logger.trace("Using MessageSource [" + this.messageSource + "]");
		}
	}
	else {
		// Use empty MessageSource to be able to accept getMessage calls.
		// 使用空MessageSource能够接受getMessage调用
		DelegatingMessageSource dms = new DelegatingMessageSource();
		dms.setParentMessageSource(getInternalParentMessageSource());
		this.messageSource = dms;
		beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
		}
	}
}



详看8——initApplicationEventMulticaster();

protected void initApplicationEventMulticaster() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	// beanFactory中包含applicationEventMulticaster
	if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
		// 取出来赋值
		this.applicationEventMulticaster =
				beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
		if (logger.isTraceEnabled()) {
			logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
		}
	}
	else {
		// 没有初始化一个简单的事件多播器
		this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
		// 注册单例到beanFactory里
		beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
					"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
		}
	}
}

看下简单事件多播器初始化做了什么

public SimpleApplicationEventMulticaster(BeanFactory beanFactory) {
	// 设置beanFactory
	setBeanFactory(beanFactory);
}



详看9——onRefresh()



ServletWebServerApplicationContext

中,onRefresh是创建webServer

protected void onRefresh() {
	super.onRefresh();
	try {
		createWebServer();
	}
	catch (Throwable ex) {
		throw new ApplicationContextException("Unable to start web server", ex);
	}
}

创建webServer详情

private void createWebServer() {
	WebServer webServer = this.webServer;
	ServletContext servletContext = getServletContext();
	// 都没获取到
	if (webServer == null && servletContext == null) {
		StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");
		// 通过ServletWebServerFactory获取Bean
		ServletWebServerFactory factory = getWebServerFactory();
		createWebServer.tag("factory", factory.getClass().toString());
		// 通过工厂获取webServer
		this.webServer = factory.getWebServer(getSelfInitializer());
		createWebServer.end();
		// 注册webServerGracefulShutdown
		getBeanFactory().registerSingleton("webServerGracefulShutdown",
				new WebServerGracefulShutdownLifecycle(this.webServer));
		// 注册webServerStartStop
		getBeanFactory().registerSingleton("webServerStartStop",
				new WebServerStartStopLifecycle(this, this.webServer));
	}
	else if (servletContext != null) {
		try {
			getSelfInitializer().onStartup(servletContext);
		}
		catch (ServletException ex) {
			throw new ApplicationContextException("Cannot initialize servlet context", ex);
		}
	}
	// 初始化属性资源
	initPropertySources();
}
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
	}
}

public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
	WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}

设置servletContextInitParams、servletConfigInitParams属性

public static void initServletPropertySources(MutablePropertySources sources,
		@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

	Assert.notNull(sources, "'propertySources' must not be null");
	// servletContextInitParams
	String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
	if (servletContext != null && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletContextPropertySource(name, servletContext));
	}
	// servletConfigInitParams
	name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
	if (servletConfig != null && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
	}
}



详看10——registerListeners();

protected void registerListeners() {
	// Register statically specified listeners first.
	// 首先注册静态指定的侦听器。
	for (ApplicationListener<?> listener : getApplicationListeners()) {
		getApplicationEventMulticaster().addApplicationListener(listener);
	}

	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let post-processors apply to them!
	// 找打所有的ApplicationListener接口实现类,都添加给多播器
	String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
	for (String listenerBeanName : listenerBeanNames) {
		getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
	}

	// Publish early application events now that we finally have a multicaster...
	// 得到早期应用事件
	Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
	this.earlyApplicationEvents = null;
	if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
		for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
			// 执行早期应用事件
			getApplicationEventMulticaster().multicastEvent(earlyEvent);
		}
	}
}



详看11——finishBeanFactoryInitialization(beanFactory);

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
	// Initialize conversion service for this context.
	// 为此上下文初始化转换服务。
	if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
			beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
		beanFactory.setConversionService(
				beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
	}

	// Register a default embedded value resolver if no BeanFactoryPostProcessor
	// (such as a PropertySourcesPlaceholderConfigurer bean) registered any before:
	// at this point, primarily for resolution in annotation attribute values.
	// 有嵌入值解析器
	if (!beanFactory.hasEmbeddedValueResolver()) {
		beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
	}

	// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
	// 初始化LoadTimeWeaverAware
	String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
	for (String weaverAwareName : weaverAwareNames) {
		getBean(weaverAwareName);
	}

	// Stop using the temporary ClassLoader for type matching.
	// 停止使用临时类加载器进行类型匹配
	beanFactory.setTempClassLoader(null);

	// Allow for caching all bean definition metadata, not expecting further changes.
	// 允许缓存所有bean定义元数据,不期望进一步更改。冻结bean定义名称
	beanFactory.freezeConfiguration();

	// Instantiate all remaining (non-lazy-init) singletons.
	// 实例化所有剩余的 (非惰性init) 单例
	beanFactory.preInstantiateSingletons();
}

实例化所有剩余的 (非惰性init) 单例方法详情:

public void preInstantiateSingletons() throws BeansException {
	if (logger.isTraceEnabled()) {
		logger.trace("Pre-instantiating singletons in " + this);
	}

	// Iterate over a copy to allow for init methods which in turn register new bean definitions.
	// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
	List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

	// Trigger initialization of all non-lazy singleton beans...
	// 循环初始化所有非惰性Bean,通过BeanName
	for (String beanName : beanNames) {
		// 获取合并的本地Bean定义
		RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
		// 不是抽象的、是单例、不是懒加载
		if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
			// 是工厂Bean,前缀有&符号为工厂Bean
			if (isFactoryBean(beanName)) {
				Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
				if (bean instanceof FactoryBean) {
					FactoryBean<?> factory = (FactoryBean<?>) bean;
					boolean isEagerInit;
					if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
						isEagerInit = AccessController.doPrivileged(
								(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
								getAccessControlContext());
					}
					else {
						isEagerInit = (factory instanceof SmartFactoryBean &&
								((SmartFactoryBean<?>) factory).isEagerInit());
					}
					// 根据上面条件判断需要初始化就初始化
					if (isEagerInit) {
						getBean(beanName);
					}
				}
			}
			else {
				// 最重要的方法。BeanFactory接口的getBean(String name)。通过这个方法初始化以及实例化Bean
				getBean(beanName);
			}
		}
	}

	// Trigger post-initialization callback for all applicable beans...
	// 触发所有适用bean的初始化后回调
	for (String beanName : beanNames) {
		Object singletonInstance = getSingleton(beanName);
		if (singletonInstance instanceof SmartInitializingSingleton) {
			StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
					.tag("beanName", beanName);
			SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
					smartSingleton.afterSingletonsInstantiated();
					return null;
				}, getAccessControlContext());
			}
			else {
				smartSingleton.afterSingletonsInstantiated();
			}
			smartInitialize.end();
		}
	}
}



详看12——finishRefresh();

protected void finishRefresh() {
	// Clear context-level resource caches (such as ASM metadata from scanning).
	// 清除上下文级别的资源缓存 (例如来自扫描的ASM元数据)
	clearResourceCaches();

	// Initialize lifecycle processor for this context.
	// 为此上下文初始化生命周期处理器
	initLifecycleProcessor();

	// Propagate refresh to lifecycle processor first.
	// 首先将刷新传播到生命周期处理器。
	getLifecycleProcessor().onRefresh();

	// Publish the final event.
	// 发布最终事件
	publishEvent(new ContextRefreshedEvent(this));

	// Participate in LiveBeansView MBean, if active.
	// 如果活跃,则参与LiveBeansView MBean。
	if (!NativeDetector.inNativeImage()) {
		LiveBeansView.registerApplicationContext(this);
	}
}

完~

求点赞、关注、建议



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