Spring 源码阅读——ClassPathXmlApplicationtext 加载Bean过程
版本 sping5.2.7 springboot 2.2.8
Beans.xml 只有一个 bean 标签
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- scope="prototype" 原型模式 就是 new 默认singleton 单例"-->
<bean id="helloWorld" class="com.example.my_code.spring.basics.pojo.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
我的简单理解
- ApplicationContext 查看类关系
// An highlighted block
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {....}
ListableBeanFactory, HierarchicalBeanFactory 这两个接口都继承 BeanFactory 这里我就简单认为 ApplicationContext是一个有很多额外功能的BeanFactory;
- ClassPathXmlApplicationContext(“Beans.xml”)
个人认为应该分两个步骤 :一读取Beans.xml 二 解析Beans.xml 并初始化 bean到 查看其构造
/**
* Create a new ClassPathXmlApplicationContext, loading the definitions
* from the given XML file and automatically refreshing the context.
* @param configLocation resource location
* @throws BeansException if context creation failed
*/
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
//内部代用如下
this(new String[] {configLocation}, true, null);
}
public ClassPathXmlApplicationContext(
String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
throws BeansException {
super(parent);
setConfigLocations(configLocations);
if (refresh) {
//内部调用
refresh();
}
}
这里我出现第一个其实在后面出现的疑惑
refresh()是 AbstractApplicationContext 类下的方法 查看类关系发现ClassPathXmlApplicationContext extends AbstractXmlApplicationContext
这里也即是子类继承父类也就是本地调用
refresh() 一切的开始
@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.
//这里个就是最主要的实现我猜测的方法 加载 xml 并将xml 解析 注入到 Ben
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
....
}
//本地调用
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
return getBeanFactory();
}
//继续调用本地的一个抽象方法 这里是我正真出现第一个疑惑的地方 他有两个实现类实现了方法 到底会调用哪一个?
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
AbstractRefreshableApplicationContext 和 GenericApplicationContext 都实现了 AbstractApplicationContext refreshBeanFactory() 方法 这是一个典型的模板方法 那么这里到底该调用哪一个方法 ,其实取决于调用者
由图可见 调用的是AbstractRefreshableApplicationContext.refreshBeanFactory() 方法 源码如下图
@Override
protected final void refreshBeanFactory() throws BeansException {
//当前存在BeanFactory 先关闭
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
//创建新的 beanFactory
//DefaultListableBeanFactory 这个类里面存在所有BeanDefinition 相关数据
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
//加载bean信息
loadBeanDefinitions(beanFactory);
this.beanFactory = beanFactory;
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
loadBeanDefinitions 调用 AbstractXmlApplicationContext
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
//让XmlBeanDefinitionReader 解析读取Bean
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);
}