这里我们以ClassPathXmlApplicationContext为例,跟踪Spring的启动过程,其实主要是跟踪AbstractApplicationContext.refresh。

1

跟踪源码来到AbstractApplicationContext#prepareRefresh中的

1
2
3
// Validate that all properties marked as required are resolvable
// see ConfigurablePropertyResolver#setRequiredProperties
getEnvironment().validateRequiredProperties();

那我们就先来看一下Environment相关的内容。

AbstractApplicationContext内置了environment变量,getEnvironment通过createEnvironment方法,将environment实例化为StandardEnvironment。下面的代码算是一个概括性描述,

1
private ConfigurableEnvironment environment = new StandardEnvironment();

StandardEnvironment重写了customizePropertySources方法,目的是在实例化时将System.getProperties()获取的系统属性以及System.getenv()获取的系统环境变量添加到propertySources中。propertySources是基于CopyOnWriteArrayList<PropertySource<?>>实现的MutablePropertySources实例。

除了重写customizePropertySources方法,其他的方法仍由AbstractEnvironment来实现,propertySources即内置于AbstractEnvironment。从上图可以看出AbstractEnvironment同时实现了EnvironmentConfigurablePropertyResolver接口,AbstractEnvironment基于LinkedHashSet实现了Environment(profile)相关的方法,而ConfigurablePropertyResolver相关的方法则由其内置的一个PropertySourcesPropertyResolver实例来实现。比如,

1
2
3
4
@Override
public void validateRequiredProperties() throws MissingRequiredPropertiesException {
this.propertyResolver.validateRequiredProperties();
}
2

接着来到AbstractApplicationContext#obtainFreshBeanFactory中的

1
refreshBeanFactory();

上述方法由AbstractRefreshableApplicationContext#refreshBeanFactory实现,销毁相关的bean(清理bean相关的对象),关闭BeanFactory,重新实例化BeanFactory。

1
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(null);

beanFactory通过内置的serializableFactories保存serializationId与其自身弱引用的对应关系。在下面提到的loadBeanDefinitions结束之后,将beanFactory赋给AbstractApplicationContext内置的beanFactory

loadBeanDefinitions方法被多次重载,最终到AbstractBeanDefinitionReader#loadBeanDefinitions(String, Set)可以分为两部分,第一部分是获取资源文件,第二部分是从资源文件中获取BeanDefinition。

  • getResources

beanFactory自身作为XmlBeanDefinitionReader内置的resourceLoader,得到XmlBeanDefinitionReader实例,同时将其environment赋值给XmlBeanDefinitionReader内置的environment

reader将getResources的任务交给其实例化时的resourceLoader,即beanFactory;beanFactory又将getResources的任务交给其内置的resourcePatternResolverPathMatchingResourcePatternResolver实例)。

  • loadBeanDefinitions

继续跟踪源码来到DefaultBeanDefinitionDocumentReader#doRegisterBeanDefinitions中的

1
2
3
4
5
6
7
8
9
10
if (this.delegate.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
return;
}
}
}

这里如果元素配置了profile属性,则会通过前面的environment检查配置的profile是否符合,如果不符合则跳过当前资源文件。

接着来到DefaultBeanDefinitionDocumentReader#processBeanDefinition中的

1
2
3
4
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
...
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());

BeanDefinitionReaderUtils#createBeanDefinition返回GenericBeanDefinition实例,设置其相应的属性,包装成BeanDefinitionHolder实例。

这里getRegistry返回的是实例化XmlBeanDefinitionReader时传入的beanFactory,所以回到DefaultListableBeanFactory#registerBeanDefinition。

关于设置GenericBeanDefinition实例的属性,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Override
public void setBeanClassName(String beanClassName) {
this.beanClass = beanClassName;
}
@Override
public String getBeanClassName() {
Object beanClassObject = this.beanClass;
if (beanClassObject instanceof Class) {
return ((Class<?>) beanClassObject).getName();
}
else {
return (String) beanClassObject;
}
}
3

接着跟踪源码来到AbstractApplicationContext#prepareBeanFactory中的,

1
2
// Configure the bean factory with context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

看一下ApplicationContextAwareProcessor重写的postProcessBeforeInitialization方法,该方法会调用ApplicationContextAwareProcessor#invokeAwareInterfaces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(
new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}

如果bean实现了以下接口,包括EnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationContextAware,则bean在初始化之前,相应的实现方法将被调用。

4

跟踪源码来到AbstractApplicationContext#invokeBeanFactoryPostProcessors,获取beanFactory中已经注册的BeanFactoryPostProcessor,调用其中的postProcessBeanFactory方法。调用之前会判断处理器是否实现了BeanDefinitionRegistryPostProcessor接口,如果实现了,则先调用其中的postProcessBeanDefinitionRegistry方法,不再调用postProcessBeanFactory方法。

1
2
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);

再使用DefaultListableBeanFactory#getBeanNamesForType获取未注册的BeanFactoryPostProcessor,重复上述过程。

5

接着跟踪源码来到AbstractApplicationContext#registerBeanPostProcessors,注册尚未注册的BeanPostProcessor,最后会重新注册实现了MergedBeanDefinitionPostProcessor接口的处理器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// First, register the BeanPostProcessors that implement PriorityOrdered.
sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
// Next, register the BeanPostProcessors that implement Ordered.
...
sortPostProcessors(beanFactory, orderedPostProcessors);
registerBeanPostProcessors(beanFactory, orderedPostProcessors);
// Now, register all regular BeanPostProcessors.
...
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(beanFactory, internalPostProcessors);
registerBeanPostProcessors(beanFactory, internalPostProcessors);

6

接着初始化MessageSourceApplicationEventMulticaster,注册监听者并广播早期的ApplicationEvents

TODO

  • DefaultListableBeanFactory#preInstantiateSingletons
  • AbstractBeanFactory#doGetBean

最后初始化LifecycleProcessor

7

Spring容器-ApplicationContext的启动过程