spring中使用ClassPathXmlApplicationContext解析为BeanDefinition过程

本文于698天之前发表,文中内容可能已经过时。

首先恭祝大家新年快乐O(∩_∩)O哈哈~

BeanDefinition是Spring配置信息的内部对象映射,ClassPathXmlApplicationContext是ApplicatinContext和BeanFactory接口实现之一,是spring最经常使用的应用上下文初始化spring框架类,那么它是如何把xml配置文件转换成BeanDefinition对象的呢,本文将介绍。

入口

我们使用如下代码进行跟踪,查看如何初始化的

1
ApplicationContext bf = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");

ClassPathXmlApplicationContext在初始化时会调用好多方法,有的会直接调用父类,父类会再调用子类等方法,所以为了更好的理解初始化,对ClassPathXmlApplicationContext的类继承结构有必要好好了解。
类继承结构

初始化配置分析

一层层跟进ClassPathXmlApplicationContext的构造方法找到如下构造方法

1
2
3
4
5
6
7
8
9
10
11
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
//进行配置文件解析器的初始化
super(parent);
//将配置文件路径信息转换成对应的resource对象
setConfigLocations(configLocations);
//进行全流程初始化
if (refresh) {
refresh();
}
}

可以看到上面最重要的方法是refresh方法,此方法包含了整个spring流程初始化,进一步跟踪如下:

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
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;
}
}
}

可以看到refresh方法定义了各个子方法进行加载,流程很清晰,其中

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

进行了配置文件解析的工作,继续跟踪obtainFreshBeanFactory方法

1
2
3
4
5
6
7
8
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}

obtainFreshBeanFactory又调用了refreshBeanFactory方法但继续点击refreshBeanFactory方法是个抽象方法,spring给的注释是交给了子类实现,根据我们上面写的继承结构,最后在AbstractRefreshableApplicationContext类中实现了refreshBeanFactory方法,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
}
}

可以看到AbstractRefreshableApplicationContext对refreshBeanFactory实现逻辑是先判断是否已经生成了BeanFactory,如果生成需要先清理已经生成的BeanFactory,然后在进行初始化。其中loadBeanDefinitions(beanFactory);就是我们要找的方法了,从命名我们也可以猜测到进行BeanDefinition装载。我们继续跟踪又是一个抽象方法交给子类实现,根据我们刚才的类结构在AbstractXmlApplicationContext类中对其实现,方法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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);
}

可以看到此方法又调用了字方法loadBeanDefinitions(beanDefinitionReader);,此方法内容如下:

1
2
3
4
5
6
7
8
9
10
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}

可以看到loadBeanDefinitions方法使用XmlBeanDefinitionReader 进行配置文件读取,感兴趣的读者可以进去进行追踪,至此spring配置文件初始化加载完毕。

想了解更多技术文章信息,请继续关注wiliam.s Blog,谢谢,欢迎来访!

欣赏此文?求鼓励,求支持!
上一篇