聊透Spring-1-ClassPathXmlApplicationContext源码解析

前言

离上次写Spring的博客有一段时间了,之前写了三篇关于Spring的博客:

从标题可以很容易看出来,主要讲了三个类BeanWrapper,BeanDefinition,BeanFactory。这三个类是spring-beans这个包里面的核心类,但是,这三篇文章在我现在看来写得不算太完整,有些理解也不算太准确。这篇博客会从ClassPathXmlApplicationContext入手,完整的走一遍Spring从xml配置文件加载Bean的过程。

Context做的事情和BeanFactory不太一样,Context是BeanFactory的超集,具有BeanFactory的所有功能。Context还提供了对Resource的访问、捕获事件等。

一、入口

1
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

ClassPathXmlApplicationContext的用法入上所示,直接看构造方法:

1
2
3
4
5
6
7
8
9
10
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {

super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}

最终使用的是重载的构造方法,具体代码如上,关键的一步是调用refresh方法,所以接下来看看refresh接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Propagate exception to caller.
throw ex;
}

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

咱们到了ClassPathXmlApplicationContext最关键的一个方法,这个方法里面包含了完整的Spring启动代码。这里先不进到具体的代码里面,先大致解释一下每个方法的作用:

  • prepareRefresh,没干什么,设置了几个标志位标志refresh开始,不是重点。
  • obtainFreshBeanFactory,很关键,首先完成了读取Xml并将配置翻译为BeanDefinition,创建了一个DefaultListableBeanFactory作为Context的BeanFactory。DefaultListableBeanFactory前面的博客中讲到过,是BeanFactory的默认实现,也是BeanFactory功能的集大成者。最后将翻译过后的BeanDefinition全部注册进BeanFactory,等待初始化。
  • prepareBeanFactory,对BeanFactory做了一些基本的设置,设置了一些默认的Bean,比如ApplicationContext,这也是为什么我们我们可以直接通过@Autowired去获取一个ApplicationContext的原因。
  • postProcessBeanFactory,一个钩子方法,或者说模板方法,留给子类实现。
  • invokeBeanFactoryPostProcessors,调用所有的BeanFactoryPostProcessor,这步会借助BeanFactory的getBean去获取BeanFactoryPostProcessor类型的Bean。
  • registerBeanPostProcessors,找出所有的BeanPostProcessor,然后注册进BeanFactory,等待会实例化和初始化Bean的时候使用。
  • initMessageSource,初始化国际化资源。
  • initApplicationEventMulticaster,初始化事件分发器。
  • onRefresh,也是一个模板方法。
  • registerListeners,找到时间监听的Bean,并注册。
  • finishBeanFactoryInitialization,重要的一个方法,里面触发了对所有Bean的初始化。
  • finishRefresh,结束了Context的refresh,修改一些标志位和发送刷新完成的事件。

接下来我们一个一个跟进这些方法,国际化、事件相关的不是本篇博客的重点。

二、obtainFreshBeanFactory方法

这个方法创建了BeanFactory,并将Xml配置中配置的Bean转换成了BeanDefinition。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//  AbstractRefreshableApplicationContext.java

@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}

跟两次就能来到这个方法,这个方法会先试图销毁已经存在的BeanFactory,然后会创建一个DefaultListableBeanFactory(可以参考spring-beans包源码阅读-4-BeanFactory)。最后调用了一个loadBeanDefinitions,这步就是从Xml配置中加载BeanDefinition了,我们稍微进去看一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// AbstractXmlApplicationContext.java
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}

可以看到创建了一个XmlBeanDefinitionReader去解析Xml,继续进入loadBeanDefinitions方法,最终可以走到XmlBeanDefinitionReader的doLoadBeanDefinition方法中:

1
2
3
4
5
6
7
8
9
// XmlBeanDefinitionReader.java
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
}
// ...
}

首先使用doLoadDocument将Xml文件通过w3c.doc工具解析成Document,这部分就不赘述了,继续看registerBeanDefinitions:

1
2
3
4
5
6
7
// XmlBeanDefinitionReader.java
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getRegistry().getBeanDefinitionCount();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}

重要的方法是registerBeanDefinitions,这个方法在BeanDefinitionDocumentReader这个类中,这个类是专门负责将Document转换成BeanDefinition的类,进去看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// DefaultBeanDefinitionDocumentReader.java
protected void doRegisterBeanDefinitions(Element root) {
// Any nested <beans> elements will cause recursion in this method. In
// order to propagate and preserve <beans> default-* attributes correctly,
// keep track of the current (parent) delegate, which may be null. Create
// the new (child) delegate with a reference to the parent for fallback purposes,
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);

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)) {
if (logger.isInfoEnabled()) {
logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
"] not matching: " + getReaderContext().getResource());
}
return;
}
}
}

preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);

this.delegate = parent;
}

最后的方法落在这里:

1
2
3
preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);

preProcessXml和postProcessXml也是两个模板方法,留给子类一个扩展的入口,核心方法是parseBeanDefinitions,最后会走到parseDefaultElement方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}

这个方法里面可以看到解析Xml的骨干方法了:

  • importBeanDefinitionResource,首先检查Xml有没有import其他的Xml配置文件,如果有,则递归解析import标签所引用的配置文件。
  • processAliasRegistration,解析alias标签。
  • processBeanDefinition,核心方法,将<bean>标签解析成一个BeanDefinition。
  • doRegisterBeanDefinitions,递归解析嵌套的<beans>标签。

所以我们只用看processBeanDefinition就可以了,最终会走到BeanDefinitionParserDelegate这个类中,这个类是更加具体的解析一个BeanDefinition的委托类,不要和刚刚的BeanDefinitionDocumentReader混淆,BeanDefinitionDocumentReader负责整体的解析流程的控制,没有涉及解析的细节,解析每个具体的BeanDefinition的任务是交给BeanDefinitionParserDelegate实现的。BeanDefinitionParserDelegate里面可以看到完整的将Xml标签转换为Definition的过程,核心方法是parseBeanDefinitionElement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// BeanDefinitionParserDelegate.java
@Nullable
public AbstractBeanDefinition parseBeanDefinitionElement(
Element ele, String beanName, @Nullable BeanDefinition containingBean) {

this.parseState.push(new BeanEntry(beanName));

String className = null;
if (ele.hasAttribute(CLASS_ATTRIBUTE)) {
className = ele.getAttribute(CLASS_ATTRIBUTE).trim();
}
String parent = null;
if (ele.hasAttribute(PARENT_ATTRIBUTE)) {
parent = ele.getAttribute(PARENT_ATTRIBUTE);
}

try {
AbstractBeanDefinition bd = createBeanDefinition(className, parent);

parseBeanDefinitionAttributes(ele, beanName, containingBean, bd);
bd.setDescription(DomUtils.getChildElementValueByTagName(ele, DESCRIPTION_ELEMENT));

parseMetaElements(ele, bd);
parseLookupOverrideSubElements(ele, bd.getMethodOverrides());
parseReplacedMethodSubElements(ele, bd.getMethodOverrides());

parseConstructorArgElements(ele, bd);
parsePropertyElements(ele, bd);
parseQualifierElements(ele, bd);

bd.setResource(this.readerContext.getResource());
bd.setSource(extractSource(ele));

return bd;
}
catch (ClassNotFoundException ex) {
error("Bean class [" + className + "] not found", ele, ex);
}
catch (NoClassDefFoundError err) {
error("Class that bean class [" + className + "] depends on not found", ele, err);
}
catch (Throwable ex) {
error("Unexpected failure during bean definition parsing", ele, ex);
}
finally {
this.parseState.pop();
}

return null;
}

到这里其实就比较简单了,首先拿到Xml标签中的class属性的值,然后就可以去拿到对应的Class创建BeanDefinition了,然后后面继续解析Xml的节点,将配置都保存在BeanDefinition中。

最后我们回到DefaultBeanDefinitionDocumentReader中的processBeanDefinition方法中,可以看到在我们获得具体BeanDefinition之后,调用registerBeanDefinition方法将BeanDefinition和classname注册进了我们的BeanFactory中,这部分代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// DefaultBeanDefinitionDocumentReader.java
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// BeanDefinitionReaderUtils.java
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {

// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}

具体解析每个标签的过程就不追究细节了。

这部分代码在《Spring源码深度解析》这本书里面有详细介绍。

三、invokeBeanFactoryPostProcessors方法

BeanFactoryPostProcessor的作用,是给用户在BeanDefinition被创建为一个个Bean实例之前做一些自定义行为的扩展接口,这个接口在一些和Spring对接的框架里面非常有用,比如我们的框架中可以自定义了各种各样的Bean,最终需要Spring帮我们去创建和管理这些Bean,那么我们可以写一个自定义的BeanFactoryPostProcessor,然后里面将我们的各种类转换为BeanDefinition,最后注册进BeanFactory,在之后BeanFactory创建Bean的时候,就会把我们自定义的BeanDefinition也创建成一个一个的Bean了。当然我们也可以通过这个接口去修改已经存在的BeanDefinition。

invokeBeanFactoryPostProcessors这个方法最终委托给了PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors方法,这个方法非常的长,但是其实很有调理,主要是通过检查我们的BeanFactory和BeanFactoryPostProcessor不同的子类,并通过不同优先级去执行所有的BeanFactoryPostProcessor。

这里有个问题,获取BeanFactoryPostProcessor是通过BeanFactory#getBean的方法获取的,这意味着如果在BeanFactoryPostProcessor中引用了其他的Bean,就会引起这些Bean提前被创建,那么某些BeanFactoryPostProcessor和BeanPostProcessor的自定义修改就不会对这些Bean生效了,这块需要特别注意,不然会引发一些奇怪的Bug。

这部分代码就不跟踪了,因为调用栈很浅,最终代码全部都在PostProcessorRegistrationDelegate这个类里面,大家自行查看吧。

四、registerBeanPostProcessors方法

BeanPostProcessors和BeanFactoryPostProcessor的作用不同,BeanPostProcessors作用在Bean的实例化、初始化的过程中。BeanPostProcessors有很多不同的扩展子类,用于创建Bean的不同生命周期中。

这里的registerBeanPostProcessors方法和invokeBeanFactoryPostProcessors方法非常类似,代码也在PostProcessorRegistrationDelegate中,只不过这里只是将所有的BeanPostProcessors通过BeanFactory#getBean找出来,然后注册进了BeanFactory中。

具体的细节也不展开了,这里的逻辑比较简单,同时要注意被BeanPostProcessors引用的Bean会提前暴露的问题。

五、finishBeanFactoryInitialization方法

最后看看这个方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// AbstractApplicationContext.java
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();
}

最关键的是最后一行:

1
beanFactory.preInstantiateSingletons();

这里加载了Spring中所有的Bean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// DefaultListableBeanFactory.java
public void preInstantiateSingletons() throws BeansException {
if (logger.isDebugEnabled()) {
logger.debug("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...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
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...
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();
}
}
}
}

这里挨个初始化了所有的Bean,也比较好懂。

当调用了getBean之后,后面的逻辑就可以参考spring-beans包源码阅读-4-BeanFactory这篇博客了。

六、总结

ClassPathXmlApplicationContext的代码还是挺简单的,这个Context负责的是纯Xml配置的Spring加载方式,之后我们会讲AnnotationConfigApplicationContext,这个Context支持的是注解配置类型的加载方式,以及会讲Xml和注解是如何混合使用的。

在讲AnnotationConfigApplicationContext之前,还会使用一篇博客回顾一下BeanFactory初始化Bean的过程。

这里有一篇宏观讲解Spring的博客,是我见过的所有写Spring博客里面最好的一篇,推荐给大家:

Spring 框架的设计理念与设计模式分析