【SpringBoot源码】核心函数之refresh、单例bean初始化逻辑

【SpringBoot源码】核心函数之refresh、单例bean初始化逻辑

Scroll Down

前言

refresh作为SpringBoot启动流程的核心之一,是必须掌握的东西,因为初始化bean等逻辑都在这里面。但偏偏refresh这个函数的源码逻辑非常复杂,我跟了几遍后,也仅仅是明白了它到底做了什么,但是还不太明白为啥要这样做。

其中,初始化bean的逻辑(finishBeanFactoryInitialization)最"套娃",我第一次跟的时候差点没口吐白沫。

另外,本次源码追踪定位仅仅是了解refresh的执行流程、还有其中单例bean初始化逻辑,希望对排错、编码思想有一个提升,对于其它细节如:tomcat的初始化、cglib代理等不会跟进。

SpringBoot Version:2.1.7/2.1.9


实验代码

定义两个对象

Student.java

@Data
@Builder
public class Student {
    private String name;
    private int age;
}

Monkey.java

@Data
public class Monkey extends Animal {
    private String name;
}

其它一些代码

MyBeanRegister.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 定义bean的名字和类型
 */

@Component
public class MyBeanRegister implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
        rootBeanDefinition.setBeanClass(Monkey.class);
        registry.registerBeanDefinition("monkey", rootBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}

MyBeanFactoryPostprocessor.java

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * 设置已经定义的bean的属性和值
 */

@Component
public class MyBeanFactoryPostprocessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition monkey = beanFactory.getBeanDefinition("monkey");
        MutablePropertyValues propertyValues1 = monkey.getPropertyValues();
        propertyValues1.addPropertyValue("name", "monkey");
    }
}

主函数

import com.wenjie.sb2.ioc.ann.Student;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Sb2Application {

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

	}

	@Bean("student")
	public Student getStudent(){
		return Student.builder().name("WenJie").age(18).build();
	}
}

上面的MyBeanFactoryPostprocessor和MyBeanRegister算是一种另类的定义bean的方式,它们覆写的方法会被回调,具体等追到相关源码就知道了,它们组合起来其实就相当于@Bean的效果。

refresh方法链

下面先找到refresh方法的所在位置,先跟进我们熟悉的SpringBootApplication#run方法:
org.springframework.boot.SpringApplication#run(java.lang.String...)

	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			prepareContext(context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
                        ...(略)

跟进refreshContext方法,之后再持续跟进refresh方法,最终来到如下代码:
org.springframework.context.support.AbstractApplicationContext#refresh

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// spring容器状态设置、初始化属性设置、检查必备属性是否存在。
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			// 更改当前beanFactory状态、设置序列化id
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			// 设置beanFactory的一些属性、添加后置处理器(如实验代码的两Processor)、
			// 注册一些组件、忽略部分自动装配接口
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				// 根据环境不同,调用子类重写以在BeanFactory完成创建后做进一步设置(第一次调用就是web环境的组件)。
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				// 调用BeanDefinitionRegistryPostProcessor、BeanFactoryPostProcessor的实现
				// 向容器内添加bean的类型、属性等定义。
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				// 找到BeanPostProcessor实现,排序,然后注册到spring容器中。
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				// 主要是负责国际化的设置
				initMessageSource();

				// 初始化广播器
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				// 创建web容器(如tomcat)
				onRefresh();

				// Check for listener beans and register them.
				// 注册监听事件
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				// 实例化单例bean
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				// 初始化生命周期处理器,调用处理器的onRefresh方法、JMX相关处理。
				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();
			}
		}
	}
  • 这纯线性函数调用,连画流程图的功夫都省下了。

上面的函数每个都会瞧一瞧,不过finishBeanFactoryInitialization是最难、最"套娃"的,第一次看可能会感到十分不适,请做好心理准备。

下面开始跟进源码。

prepareRefresh

跟进prepareRefresh方法:
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext#prepareRefresh

	@Override
	protected void prepareRefresh() {
		this.scanner.clearCache();
		super.prepareRefresh();
	}

首先会清理一些缓存,继续跟进prepareRefresh方法:
org.springframework.context.support.AbstractApplicationContext#prepareRefresh

	protected void prepareRefresh() {
		// 开启计时并设置容器状态
		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...
		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...
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

这里先跟进initPropertySources方法:
org.springframework.web.context.support.GenericWebApplicationContext#initPropertySources

	protected void initPropertySources() {
		ConfigurableEnvironment env = getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
		}
	}
  • 第一次进到这个方法serverletContext、serverletConfig都为null,所以其实相当于啥都没做。

返回到上面的prepareRefresh方法,再继续跟进validateRequiredProperties方法:
org.springframework.core.env.AbstractEnvironment#validateRequiredProperties

	@Override
	public void validateRequiredProperties() throws MissingRequiredPropertiesException {
		this.propertyResolver.validateRequiredProperties();
	}

继续跟进validateRequiredProperties方法:
org.springframework.core.env.AbstractPropertyResolver#validateRequiredProperties

	@Override
	public void validateRequiredProperties() {
		MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();
		for (String key : this.requiredProperties) {
			if (this.getProperty(key) == null) {
				ex.addMissingRequiredProperty(key);
			}
		}
		// 如果有漏缺的必备属性,则抛出异常
		if (!ex.getMissingRequiredProperties().isEmpty()) {
			throw ex;
		}
	}

其中this.requiredProperties就是所谓的必备属性,我们可以在application.properties/yml配置文件中配置如下必备属性:

Require=RequireValue

之后再断点调试看看值,确实是检测到必备属性了:
断点调试.png


小小结:prepareRfresh主要做了什么?

  • spring容器状态设置、初始化属性设置、检查必备属性是否存在。

obtainFreshBeanFactory

视角重新转回到refresh方法中,跟进obtainFreshBeanFactory方法:
org.springframework.context.support.AbstractApplicationContext#obtainFreshBeanFactory

	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		refreshBeanFactory();
		return getBeanFactory();
	}

先跟进refreshBeanFactory方法看看:
org.springframework.context.support.GenericApplicationContext#refreshBeanFactory

	@Override
	protected final void refreshBeanFactory() throws IllegalStateException {
		// cas操作更新状态
		if (!this.refreshed.compareAndSet(false, true)) {
			throw new IllegalStateException(
					"GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
		}
		// 设置序列化id
		this.beanFactory.setSerializationId(getId());
	}

返回到obtainFreshBeanFactory方法,跟进getBeanFactory看看:
org.springframework.context.support.GenericApplicationContext#getBeanFactory

	@Override
	public final ConfigurableListableBeanFactory getBeanFactory() {
		return this.beanFactory;
	}
  • 注意,这里返回的是DefaultListableBeanFactory实例,后面还会遇到它。

小小结:obtainFreshBeanFactory主要做了什么?

  • 更新beanFactory的状态,再返回beanFactory。

prepareBeanFactory

视角在此拉回refresh方法,跟进prepareBeanFactory方法:
org.springframework.context.support.AbstractApplicationContext#prepareBeanFactory

	protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		beanFactory.setBeanClassLoader(getClassLoader());
		// spel表达式解析器
		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 interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		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.
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		if (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.
		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());
		}
	}
  • addBeanPostProcessor:添加bean后置处理器。
  • ignoreDependencyInterface:忽略自动装配的依赖关系。
  • registerResolvableDependency:比如beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);,它的意思就是当我们需要BeanFactory的实例时,就返回后面设置的beanFactory实例。
  • 下面的if代码就是负责检查某些bean并注册。

关于bean后置处理器,后面还会遇到。


小小结:postProcessBeanFactory主要做了什么?

  • 设置beanFactory的一些属性,比如spel表达式解析器等。
  • 设置了一些后置处理器,后面会用到这些后置处理器(不过仅仅提到,不会深入到具体实现逻辑)。
  • 解除了一些自动装配的依赖关系,并注册了一些组件。

invokeBeanFactoryPostProcessors

视角回到refresh方法,继续跟进invokeBeanFactoryPostProcessors方法:
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

	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 (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}
	}

跟进invokeBeanFactoryPostProcessors方法看看:
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

	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 (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}
	}

看看getBeanFactoryPostProcessors返回了什么:
getBeanFactoryPostProcessors返回集合.png

在上上面的代码我们遇到了bean的后置处理器,而在这里,我们遇到了beanFactory的后置处理器,下面尝试找找beanFactory后置处理器的来源。

这里先跟进getBeanFactoryPostProcessors方法看看:
org.springframework.context.support.AbstractApplicationContext#getBeanFactoryPostProcessors

	public List<BeanFactoryPostProcessor> getBeanFactoryPostProcessors() {
		return this.beanFactoryPostProcessors;
	}

在getBeanFactoryPostProcessors的当前类,我们能找到addBeanFactoryPostProcessor方法,跟根被使用到的地方:
addBeanFactoryPostProcessor被调用的地方.png

随便挑一个getBeanFactoryPostProcessors返回的,跟进去:

class SharedMetadataReaderFactoryContextInitializer
		implements ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {

	public static final String BEAN_NAME = "org.springframework.boot.autoconfigure."
			+ "internalCachingMetadataReaderFactory";

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor());
	}
        ...(略)

可见对于getBeanFactoryPostProcessors(剩下两个后置处理器同样)返回的后置处理器,SpringBoot是利用初始化器来初始化的,这正好和我前面初始化器的博客连接起来了。

知道getBeanFactoryPostProcessors的返回值之后,我们返回再继续跟进invokeBeanFactoryPostProcessors方法,
org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)

        // 下面只贴出一部分,因为实际源码有200行以上,但只要理解了其中一段,剩下的都很好理解。
	public static void invokeBeanFactoryPostProcessors(
			ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

		// Invoke BeanDefinitionRegistryPostProcessors first, if any.
		Set<String> processedBeans = new HashSet<>();

		if (beanFactory instanceof BeanDefinitionRegistry) {
			BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
			List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
			List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

			for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
				if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
					BeanDefinitionRegistryPostProcessor registryProcessor =
							(BeanDefinitionRegistryPostProcessor) postProcessor;
					registryProcessor.postProcessBeanDefinitionRegistry(registry);
					registryProcessors.add(registryProcessor);
				}
				else {
					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.
			String[] postProcessorNames =
					beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
				}
			}
			// 对处理器排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			// 保存到registryProcessors集合
			// registryProcessors集合到最后还会被遍历一次,回调对应的处理器方法做相应操作。
			registryProcessors.addAll(currentRegistryProcessors);
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();
			...(略)
			// 剩余的代码就是把beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);或isTypeMatch匹配的实现类换掉,然后继续匹配其它实现类来做相应处理,操作都类似与上上面这段代码,知道所有应该被处理的后置处理器都被处理了。

上述的postProcessBeanDefinitionRegistry方法做的事,其实就类似于我们实验代码MyBeanRegister.java做的事,跟进invokeBeanDefinitionRegistryPostProcessors方法,发现里面也就是遍历调用处理器的postProcessBeanDefinitionRegistry方法,这个方法做的事大概就是注册、定义bean类型等操作了。

我们可以用断点调试一下,看看是不是真的这样:
image.png
继续跟进
image.png

除了上面贴出来的invokeBeanDefinitionRegistryPostProcessors外,还有一个invokeBeanFactoryPostProcessors,它的作用就是遍历处理器并回调其postProcessBeanFactory方法做某些操作,回调逻辑类似上面的postProcessBeanDefinitionRegistry,只是具体作用随具体实现而不同。


小小结:invokeBeanFactoryPostProcessors主要做了什么?

  • 回调符合要求的BeanDefinitionRegistryPostProcessor实现并向容器内添加bean类型等定义。
  • 回调符合要求的BeanFactoryPostProcessor实现并向容器内bean做定义、添加属性等操作。

registerBeanPostProcessors

返回到refresh方法,跟进registerBeanPostProcessors方法:
org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors

	protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
		PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
	}

继续跟进registerBeanPostProcessors方法:
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.support.AbstractApplicationContext)

	public static void registerBeanPostProcessors(
			ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

		// 获取实现了BeanPostProcessor接口的后置处理器。
		// 实验代码MyBeanPostProcessor就实现了BeanPostProcessor接口。
		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.
		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.
		sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
		registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

		// Next, register the BeanPostProcessors that implement Ordered.
		List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>();
		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.
		List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>();
		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.
		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).
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
	}

上面的代码大致就是根据不同实现接口,排序、分批注册bean后置处理器,其中就包括了实验代码的MyBeanPostProcessor:
image.png


小小结:registerBeanPostProcessors主要做了什么?

  • 将bean后置处理器(BeanPostProcessor)排序然后注册到容器中。

initMessageSource

视角返回到refresh方法,跟进initMessageSource方法:
org.springframework.context.support.AbstractApplicationContext#initMessageSource

	protected void initMessageSource() {
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		// 检查国际化配置bean是否存在
		if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
			this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
			// Make MessageSource aware of parent MessageSource.
			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 + "]");
			}
		}
		// 没有则创建DelegatingMessageSource
		else {
			// Use empty MessageSource to be able to accept getMessage calls.
			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 + "]");
			}
		}
	}

代码逻辑没什么难得,就是国际化配置,如果遇到国际化配置相关的错误,可以考虑跟进这段代码看看。

initApplicationEventMulticaster

视角拉回到refresh方法,跟进initApplicationEventMulticaster方法:
org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster

	protected void initApplicationEventMulticaster() {
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		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.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() + "]");
			}
		}
	}

就是初始化广播器bean,没啥特别难的地方,广播器的调用在【监听器分析】一节有跟进过源码,有兴趣的可以去看看。

onRefresh(待填坑)

目前只知道是初始化web容器(默认tomcat),由于我现在对tomcat不太了解,暂时不打算追,打算日后找到一些资料或进行过相关学习之后再补充这一块。

registerListeners

org.springframework.context.support.AbstractApplicationContext#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!
		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 (earlyEventsToProcess != null) {
			for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
				getApplicationEventMulticaster().multicastEvent(earlyEvent);
			}
		}
	}

这个方法就是负责注册监听器到广播器中,监听器回调源码流程也可以参考【监听器分析】.

顺便再提一下earlyApplicationEvents,如果监听器还没注册,系统就尝试广播事件,那么这个事件集就会被存到earlyApplicationEvents中,等到上述代码完成监听器注册后,再重新广播事件。

finishBeanFactoryInitialization

开始跟进

关于这一方法的逻辑,我并没有自信能讲清楚它的逻辑,实际上我自己也只是能看懂代码逻辑,由于套娃太多,要清楚地讲出来可就有难度了。如果你看不太懂的话,你只需要知道这个方法负责的是:初始化剩余所有的单例bean。一般用户自定义的bean都会在这个方法里面初始化,还有部分系统bean则是在前面需要的时候被初始化了。

好了,废话不多说,跟进源码瞧一瞧:
org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization

	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 bean post-processor
		// (such as a PropertyPlaceholderConfigurer 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.
		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.
		beanFactory.freezeConfiguration();

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

跟进freezeConfiguration方法看看,在这之上的代码都不是重点:
org.springframework.beans.factory.support.DefaultListableBeanFactory#freezeConfiguration

	@Override
	public void freezeConfiguration() {
		// 标记:当前正在实例化
		this.configurationFrozen = true;
		// 保存要实例化bean的beanName集合
		this.frozenBeanDefinitionNames = StringUtils.toStringArray(this.beanDefinitionNames);
	}

frozenBeanDefinitionNames、beanDefinitionNames存储的就是一下内容,其中包括实验代码的student和monkey等:
image.png
image.png

保存这些beanName的主要是为了声明这些bean准备要拿去实例化了。

返回到的finishBeanFactoryInitialization方法,跟进preInstantiateSingletons方法:
org.springframework.beans.factory.support.DefaultListableBeanFactory#preInstantiateSingletons

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

		// 拿到要初始化的bean的beanName
		List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		// 遍历beanNames,逐步构造
		for (String beanName : beanNames) {
			// 拿到bean的元数据,下面会介绍这是个啥东西
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
				if (isFactoryBean(beanName)) {
					// 如果是FactoryBean,则加上前缀符
					Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
					if (bean instanceof FactoryBean) {
						final 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 {
 					// 实验代码会进入这里
					getBean(beanName);
				}
			}
		}

		// Trigger post-initialization callback for all applicable beans...
                // bean初始化后的回调方法。跟前面讲到的postProcessBeanDefinitionRegistry原理相似。
                // 用户可实现SmartInitializingSingleton接口+覆写afterSingletonsInstantiated方法达到自定义回调逻辑
		for (String beanName : beanNames) {
			Object singletonInstance = getSingleton(beanName);
			if (singletonInstance instanceof SmartInitializingSingleton) {
				final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}
	}

上面代码遇到的RootBeanDefinition非常关键,它主要发挥BeanDefinition定义的作用,bean要实例化时所需要的信息基本都在这里面了,关键信息包括属性、属性值、是什么类等,下面就通过断点调试简单看看它记录的属性和属性值:

image.png
image.png

  • 后面我们还会遇到RootBeanDefinition。

如果你对RootBeanDefinition具体存储了哪些关键信息,可以参考BeanDefinition的接口。

视角转移回上边的代码中,跟进getBean方法:
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)

	@Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

继续跟进doGetBean方法:
org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

	protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
			@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {

		// 如果是FactoryBean,则需要经过一系列转化得出beanName(其中包括删除前面设置的前缀符号等操作)
		final String beanName = transformedBeanName(name);
		Object bean;

		// Eagerly check singleton cache for manually registered singletons.
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				if (isSingletonCurrentlyInCreation(beanName)) {
					logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
							"' that is not fully initialized yet - a consequence of a circular reference");
				}
				else {
					logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
				}
			}
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}
		// 由于启动时是第一次,所有缓存不可能有,跳过上面的if块
		else {
			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			// 重新校验bean是否被实例化了。
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			// 现在是第一次启动BeanFactory已经是顶级,故getParentBeanFactory返回null
			// 于是我们可以跳过下面这段if了
			BeanFactory parentBeanFactory = getParentBeanFactory();
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				String nameToLookup = originalBeanName(name);
				if (parentBeanFactory instanceof AbstractBeanFactory) {
					return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
							nameToLookup, requiredType, args, typeCheckOnly);
				}
				else if (args != null) {
					// Delegation to parent with explicit args.
					return (T) parentBeanFactory.getBean(nameToLookup, args);
				}
				else if (requiredType != null) {
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
				else {
					return (T) parentBeanFactory.getBean(nameToLookup);
				}
			}

                        // typeCheckOnly为false
			if (!typeCheckOnly) {
				// 标记当前bean即将要实例化了
				markBeanAsCreated(beanName);
			}

			try {
				//  拿到元数据
				final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				String[] dependsOn = mbd.getDependsOn();
				// 检查当前bean头上有没有@DependsOn注解
				// 如果有指定@DependsOn,那么先去初始化@DependsOn指定的类
				if (dependsOn != null) {
					for (String dep : dependsOn) {
						if (isDependent(beanName, dep)) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
						}
						registerDependentBean(dep, beanName);
						try {
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// 如果该bean是单例则进入
				if (mbd.isSingleton()) {
					// getSingleton中会再一次检查bean是否被实例化了(同步锁)
					// 在getSingleton中还会回调这里设置的匿名实现
					sharedInstance = getSingleton(beanName, () -> {
						try {
							return createBean(beanName, mbd, args);
						}
						catch (BeansException ex) {
							// Explicitly remove instance from singleton cache: It might have been put there
							// eagerly by the creation process, to allow for circular reference resolution.
							// Also remove any beans that received a temporary reference to the bean.
							destroySingleton(beanName);
							throw ex;
						}
					});
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}

				else if (xxxx)
				...(略)逻辑与上面大致相似。
		return (T) bean;
	}

跟进createBean方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])

	@Override
	protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

		if (logger.isTraceEnabled()) {
			logger.trace("Creating instance of bean '" + beanName + "'");
		}
		RootBeanDefinition mbdToUse = mbd;

		// Make sure bean class is actually resolved at this point, and
		// clone the bean definition in case of a dynamically resolved Class
		// which cannot be stored in the shared merged bean definition.
		// 设置字节码对象
		Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
		if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
			mbdToUse = new RootBeanDefinition(mbd);
			mbdToUse.setBeanClass(resolvedClass);
		}

		// Prepare method overrides.
		try {
			mbdToUse.prepareMethodOverrides();
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
					beanName, "Validation of method overrides failed", ex);
		}

		try {
			// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
			Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
			if (bean != null) {
				return bean;
			}
		}
		catch (Throwable ex) {
			throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
					"BeanPostProcessor before instantiation of bean failed", ex);
		}

		try {
			// 跟进doCreateBean方法
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isTraceEnabled()) {
				logger.trace("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
		catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
			// A previously detected exception with proper bean creation context already,
			// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
			throw ex;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
		}
	}

关于上面的resolveBeforeInstantiation方法,其实就是通过回调来构造bean,有兴趣的可以自己跟进下,跟实验代码MyBeanFactoryPostprocessor、MyBeanRegister等原理相似,用户可以通过实现InstantiationAwareBeanPostProcessor+覆写相关方法达到自定义构造。(虽然对于用户来说,没人会用这种方式就对了)

跟进doCreateBean方法,此处【坐标1】
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean

	protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
			throws BeanCreationException {

		// Instantiate the bean.
		BeanWrapper instanceWrapper = null;
		if (mbd.isSingleton()) {
			instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
		}
		if (instanceWrapper == null) {
			// 正式构造bean,返回封装后的对象
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		// 获取bean对象,最终返回的就是这个bean
		final Object bean = instanceWrapper.getWrappedInstance();
		// 获取字节码对象
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}

		// Allow post-processors to modify the merged bean definition.
		synchronized (mbd.postProcessingLock) {
			if (!mbd.postProcessed) {
				try {
					// 回调,用户可实现对应接口+覆写对象方法,达到在此处嵌入一些操作
					applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
				}
				catch (Throwable ex) {
					throw new BeanCreationException(mbd.getResourceDescription(), beanName,
							"Post-processing of merged bean definition failed", ex);
				}
				mbd.postProcessed = true;
			}
		}

		// Eagerly cache singletons to be able to resolve circular references
		// even when triggered by lifecycle interfaces like BeanFactoryAware.
		boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
				isSingletonCurrentlyInCreation(beanName));
		if (earlySingletonExposure) {
			if (logger.isTraceEnabled()) {
				logger.trace("Eagerly caching bean '" + beanName +
						"' to allow for resolving potential circular references");
			}
			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
		}

		// Initialize the bean instance.
		Object exposedObject = bean;
		try {
			populateBean(beanName, mbd, instanceWrapper);
			// 初始化bean,包含设置属性值等操作。
			exposedObject = initializeBean(beanName, exposedObject, mbd);
		}
		catch (Throwable ex) {
			if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
				throw (BeanCreationException) ex;
			}
			else {
				throw new BeanCreationException(
						mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
			}
		}

		if (earlySingletonExposure) {
			Object earlySingletonReference = getSingleton(beanName, false);
			if (earlySingletonReference != null) {
				if (exposedObject == bean) {
					exposedObject = earlySingletonReference;
				}
				else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
					String[] dependentBeans = getDependentBeans(beanName);
					Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);
					for (String dependentBean : dependentBeans) {
						if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
							actualDependentBeans.add(dependentBean);
						}
					}
					if (!actualDependentBeans.isEmpty()) {
						throw new BeanCurrentlyInCreationException(beanName,
								"Bean with name '" + beanName + "' has been injected into other beans [" +
								StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
								"] in its raw version as part of a circular reference, but has eventually been " +
								"wrapped. This means that said other beans do not use the final version of the " +
								"bean. This is often the result of over-eager type matching - consider using " +
								"'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
					}
				}
			}
		}

		// Register bean as disposable.
		try {
			registerDisposableBeanIfNecessary(beanName, bean, mbd);
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
		}

		return exposedObject;
	}

先跟进createBeanInstance方法,只贴出关键的代码:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBeanInstance

	protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
		...(略)
		// Candidate constructors for autowiring?
		// 获取构造器集合
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
			return autowireConstructor(beanName, mbd, ctors, args);
		}

		// Preferred constructors for default construction?
		ctors = mbd.getPreferredConstructors();
		if (ctors != null) {
			return autowireConstructor(beanName, mbd, ctors, null);
		}

		// No special handling: simply use no-arg constructor.
		// 实验代码会进入此处
		return instantiateBean(beanName, mbd);
  • determineConstructorsFromBeanPostProcessors里面也是前面多次提到的回调方法,也就是说,用户可以实现对应接口+覆写对应方法,改写一些策略。

跟进instantiateBean方法:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#instantiateBean

	protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) {
		try {
			Object beanInstance;
			final BeanFactory parent = this;
			if (System.getSecurityManager() != null) {
				beanInstance = AccessController.doPrivileged((PrivilegedAction<Object>) () ->
						getInstantiationStrategy().instantiate(mbd, beanName, parent),
						getAccessControlContext());
			}
			else {
				// 实验代码会进入这里
                                // getInstantiationStrategy返回构造策略,这里会返回cglib的策略
				beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
			}
			BeanWrapper bw = new BeanWrapperImpl(beanInstance);
			// 初始化包装器。
			initBeanWrapper(bw);
			return bw;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
		}
	}

继续跟进instantiate方法:
org.springframework.beans.factory.support.SimpleInstantiationStrategy#instantiate(org.springframework.beans.factory.support.RootBeanDefinition, java.lang.String, org.springframework.beans.factory.BeanFactory)

	@Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
		// Don't override the class with CGLIB if no overrides.
		if (!bd.hasMethodOverrides()) {
			Constructor<?> constructorToUse;
			synchronized (bd.constructorArgumentLock) {
				constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
				if (constructorToUse == null) {
					final Class<?> clazz = bd.getBeanClass();
					if (clazz.isInterface()) {
						throw new BeanInstantiationException(clazz, "Specified class is an interface");
					}
					try {
						if (System.getSecurityManager() != null) {
							constructorToUse = AccessController.doPrivileged(
									(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
						}
						else {
							constructorToUse = clazz.getDeclaredConstructor();
						}
						bd.resolvedConstructorOrFactoryMethod = constructorToUse;
					}
					catch (Throwable ex) {
						throw new BeanInstantiationException(clazz, "No default constructor found", ex);
					}
				}
			}
			// 实验代码会来到这里,前面的代码都是为了拿到构造器。
			return BeanUtils.instantiateClass(constructorToUse);
		}
		else {
			// Must generate CGLIB subclass.
			return instantiateWithMethodInjection(bd, beanName, owner);
		}
	}

继续跟进instantiateClass方法,就已经到jdk底层了,都是一些反射的知识,我就不再一一赘述了。

好了,现在我们的bean总算是完成构造了。

对于monkey,则是调用构造方法构造,而student则是根据元数据记录的getStudent方法构造。

小结

如果只是实例化bean,肯定很简单,取到底层都是jdk原生的反射机制,而spring实例化bean的代码之所以让人觉得很难消化,根本原因在于中间的"干扰钩子"太多,几乎在bean实例化的每一个阶段(分得很细),都"嵌入"了一些回调机制,这就导致看源码的时候干扰非常多。

虽说有些回调机制干扰比较大,但是这些回调却又缺一不可,因为有了这些回调机制,用户就可以自定义代码"入侵"到实例化的任意阶段(实现对应接口+覆写方法),如果习惯了这种设计模式,相信看源码会顺畅很多,自己写代码的时候也可以考虑使用这种回调机制,暴露"入侵"接口。

finishRefresh

这个方法没有什么太多要注意的地方,主要是发送了两事件:
org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#finishRefresh

	@Override
	protected void finishRefresh() {
		super.finishRefresh();
		WebServer webServer = startWebServer();
		if (webServer != null) {
			publishEvent(new ServletWebServerInitializedEvent(webServer, this));
		}
	}
  • 初始化了WebServer,然后发出ServletWebServerInitializedEvent。

继续跟进super.finishRefresh:
org.springframework.context.support.AbstractApplicationContext#finishRefresh

	protected void finishRefresh() {
		// Clear context-level resource caches (such as ASM metadata from scanning).
		clearResourceCaches();

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

		// Propagate refresh to lifecycle processor first.
		// 调用生命周期处理器的onRefresh方法
		getLifecycleProcessor().onRefresh();

		// Publish the final event.
		publishEvent(new ContextRefreshedEvent(this));

		// Participate in LiveBeansView MBean, if active.
		LiveBeansView.registerApplicationContext(this);
	}
  • 这里还会发送ContextRefreshedEvent。

由于本节只是着重分析refresh方法的逻辑、bean的初始化流程,所以对于剩下的其它细节逻辑,就不一一跟踪了,以后有机会再补上,现在算是留下了一些思路。