Spring原始碼剖析1:初探Spring IOC核心流程
本文大致地介紹了IOC容器的初始化過程,只列出了比較重要的過程和程式碼,可以從中看出IOC容器執行的大致流程。
接下來的文章會更加深入剖析Bean容器如何解析xml,註冊和初始化bean,以及如何獲取bean例項等詳細的過程。
轉自:
1\. 初始化
大致單步跟了下Spring IOC的初始化過程,整個脈絡很龐大,初始化的過程主要就是讀取XML資源,並解析,最終註冊到Bean Factory中:
[![]( "flow")]( "flow")
在完成初始化的過程後,Bean們就在BeanFactory中蓄勢以待地等呼叫了。下面透過一個具體的例子,來詳細地學習一下初始化過程,例如當載入下面一個bean:
```
<bean id="XiaoWang" class="com.springstudy.talentshow.SuperInstrumentalist">
<property name="instruments">
<list>
<ref bean="piano"/>
<ref bean="saxophone"/>
</list>
</property>
</bean>
```
載入時需要讀取、解析、註冊bean,這個過程具體的呼叫棧如下所示:
[![]( "load")]( "load")
下面對每一步的關鍵的程式碼進行詳細分析:
#### 準備
儲存配置位置,並重新整理
在呼叫ClassPathXmlApplicationContext後,先會將配置位置資訊儲存到configLocations,供後面解析使用,之後,會呼叫`AbstractApplicationContext`的refresh方法進行重新整理:
```
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh,
ApplicationContext parent) throws BeansException {
super(parent);
// 儲存位置資訊,比如`com/springstudy/talentshow/talent-show.xml`
setConfigLocations(configLocations);
if (refresh) {
// 重新整理
refresh();
}
}
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) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
```
建立載入BeanFactory
```
protected final void refreshBeanFactory() throws BeansException {
// ... ...
DefaultListableBeanFactory beanFactory = createBeanFactory();
// ... ...
loadBeanDefinitions(beanFactory);
// ... ...
}
```
建立XMLBeanDefinitionReader
```
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// ... ...
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
```
#### 讀取
建立處理每一個resource
```
public int loadBeanDefinitions(String location, Set<Resource> actualResources)
throws BeanDefinitionStoreException {
// ... ...
// 透過Location來讀取Resource
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
int loadCount = loadBeanDefinitions(resources);
// ... ...
}
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
Assert.notNull(resources, "Resource array must not be null");
int counter = 0;
for (Resource resource : resources) {
// 載入每一個resource
counter += loadBeanDefinitions(resource);
}
return counter;
}
```
處理XML每個元素
```
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
// ... ...
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
// 處理每個xml中的元素,可能是import、alias、bean
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
// ... ...
}
```
解析和註冊bean
```
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));
}
}
```
本步驟中,透過parseBeanDefinitionElement將XML的元素解析為BeanDefinition,然後存在BeanDefinitionHolder中,然後再利用BeanDefinitionHolder將BeanDefinition註冊,實質就是把BeanDefinition的例項put進BeanFactory中,和後面將詳細的介紹解析和註冊過程。
#### 解析
[![]( "process")]( "process")
處理每個Bean的元素
```
public AbstractBeanDefinition parseBeanDefinitionElement(
Element ele, String beanName, BeanDefinition containingBean) {
// ... ...
// 建立beandefinition
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());
// 處理“Constructor”
parseConstructorArgElements(ele, bd);
// 處理“Preperty”
parsePropertyElements(ele, bd);
parseQualifierElements(ele, bd);
// ... ...
}
```
處理屬性的值
```
public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
String elementName = (propertyName != null) ?
"<property> element for property '" + propertyName + "'" :
"<constructor-arg> element";
// ... ...
if (hasRefAttribute) {
// 處理引用
String refName = ele.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(refName)) {
error(elementName + " contains empty 'ref' attribute", ele);
}
RuntimeBeanReference ref = new RuntimeBeanReference(refName);
ref.setSource(extractSource(ele));
return ref;
}
else if (hasValueAttribute) {
// 處理值
TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));
valueHolder.setSource(extractSource(ele));
return valueHolder;
}
else if (subElement != null) {
// 處理子型別(比如list、map等)
return parsePropertySubElement(subElement, bd);
}
// ... ...
}
```
1.4 註冊
```
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);
}
}
}
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
// ......
// 將beanDefinition註冊
this.beanDefinitionMap.put(beanName, beanDefinition);
// ......
}
```
註冊過程中,最核心的一句就是:this.beanDefinitionMap.put(beanName, beanDefinition),也就是說註冊的實質就是以beanName為key,以beanDefinition為value,將其put到HashMap中。
#### 註冊
```
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);
}
}
}
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
// ......
// 將beanDefinition註冊
this.beanDefinitionMap.put(beanName, beanDefinition);
// ......
```
理解了以上兩個過程,我們就可以自己實現一個簡單的Spring框架了。於是,我根據自己的理解實現了一個簡單的IOC框架Simple Spring,有興趣可以看看。
註冊過程中,最核心的一句就是:`this.beanDefinitionMap.put(beanName, beanDefinition)`,也就是說註冊的實質就是以beanName為key,以beanDefinition為value,將其put到HashMap中。
### 注入依賴
當完成初始化IOC容器後,如果bean沒有設定lazy-init(延遲載入)屬性,那麼bean的例項就會在初始化IOC完成之後,及時地進行初始化。初始化時會先建立例項,然後根據配置利用反射對例項進行進一步操作,具體流程如下所示:
[![]( "bean_flow")]( "bean_flow")
建立bean的例項
建立bean的例項過程函式呼叫棧如下所示:
[![]( "create_bean")]( "create_bean")
注入bean的屬性
注入bean的屬性過程函式呼叫棧如下所示:
[![]( "inject_property")]( "inject_property")
在建立bean和注入bean的屬性時,都是在doCreateBean函式中進行的,我們重點看下:
```
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd,
final Object[] args) {
// Instantiate the bean.
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
// 建立bean的例項
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// ... ...
// Initialize the bean instance.
Object exposedObject = bean;
try {
// 初始化bean的例項,如注入屬性
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
// ... ...
}
```
理解了以上兩個過程,我們就可以自己實現一個簡單的Spring框架了。於是,我根據自己的理解實現了一個簡單的IOC框架[Simple Spring](),有興趣可以看看。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69906029/viewspace-2654861/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring原始碼剖析2:初探Spring IOC核心流程Spring原始碼
- Spring IOC容器核心流程原始碼分析Spring原始碼
- Spring IOC原始碼深度剖析:Spring IoC迴圈依賴問題Spring原始碼
- Spring 高階原始碼核心思想:Spring IoCSpring原始碼
- Spring原始碼學習之路---IOC初探(二)Spring原始碼
- Spring原始碼剖析3:Spring IOC容器的載入過程Spring原始碼
- Spring原始碼剖析9:Spring事務原始碼剖析Spring原始碼
- Spring IOC 容器預啟動流程原始碼探析Spring原始碼
- Spring原始碼解析02:Spring IOC容器之XmlBeanFactory啟動流程分析和原始碼解析Spring原始碼XMLBean
- Spring:原始碼解讀Spring IOC原理Spring原始碼
- Spring原始碼剖析6:Spring AOP概述Spring原始碼
- 【spring 原始碼】IOC 之 ClassPathXmlApplicationContextSpring原始碼XMLAPPContext
- spring-IOC容器原始碼分析(二)BeanDefinition註冊流程Spring原始碼Bean
- 手寫Spring ioc 框架,狠狠的“Spring 原始碼Spring框架原始碼
- Spring原始碼分析:Spring IOC容器初始化Spring原始碼
- Spring原始碼剖析8:Spring事務概述Spring原始碼
- Spring系列(一):Spring MVC bean 解析、註冊、例項化流程原始碼剖析SpringMVCBean原始碼
- 【Spring專場】「IOC容器」不看原始碼就帶你認識核心流程以及運作原理Spring原始碼
- Spring原始碼系列:初探底層,手寫SpringSpring原始碼
- Spring5原始碼解析系列一——IoC容器核心類圖Spring原始碼
- spring-IOC容器原始碼分析(一)bean初始化流程Spring原始碼Bean
- 2.2 spring5原始碼 -- ioc載入的整體流程Spring原始碼
- Spring原始碼分析之IoC(一)Spring原始碼
- Spring原始碼分析之IoC(二)Spring原始碼
- Spring-IOC原始碼淺析Spring原始碼
- Spring原始碼之IOC(一)BeanDefinition原始碼解析Spring原始碼Bean
- Spring5原始碼 - Spring IOC 註解複習Spring原始碼
- Spring原始碼剖析開篇:什麼是Spring?Spring原始碼
- Spring原始碼解讀(1)-IOC容器BeanDefinition的載入Spring原始碼Bean
- Spring原始碼分析——spring原始碼核心方法refresh()介紹Spring原始碼
- Spring AOP 原理原始碼深度剖析Spring原始碼
- spring cloud gateway 原始碼解析(1)整體流程SpringCloudGateway原始碼
- Spring原始碼閱讀-IoC容器解析Spring原始碼
- spring原始碼解析之IOC容器(一)Spring原始碼
- 深入理解Spring IOC原始碼分析Spring原始碼
- 《Spring原始碼分析》IOC的實現Spring原始碼
- Spring 原始碼 (2)Spring IOC 容器 前戲準備工作Spring原始碼
- Spring基礎 - Spring核心之控制反轉(IOC)Spring