Spring源碼------IoC源碼分析詳解

Spring源碼------IoC源碼分析詳解

目錄

Spring源碼------IoC源碼分析詳解

1、控制反轉(IoC)

2、Spring IoC 源碼時序圖(詳細版)

3、源碼追蹤

3.1詳細版鏈接   

3.2 個人總結濃縮版(方便記憶)

4、總結


1、控制反轉(IoC)

控制反轉(IoC)原理的Spring Framework實現。IoC也稱爲依賴注入(DI)。這是一個過程,通過這個過程,對象只能通過構造函數參數,工廠方法的參數或在構造或從工廠方法返回後在對象實例上設置的屬性來定義它們的依賴關係(即,它們使用的其他對象)。 。然後容器在創建bean時注入這些依賴項。此過程基本上是bean本身的逆(因此名稱,控制反轉),通過使用類的直接構造或諸如服務定位器模式的機制來控制其依賴關係的實例化或位置。(官方文檔5.x說明)。所謂IoC,對於spring框架來說,就是由spring來負責控制對象的生命週期和對象間的關係。

基於XML的元數據不是唯一允許的配置元數據形式。Spring IoC容器本身完全與實際編寫此配置元數據的格式分離。目前,許多開發人員爲其Spring應用程序選擇 基於Java的配置

流傳的大部分博客對spring生命週期的看法

不知道Spring官方對Bean的生命問題是否有明確的定義或者解析,但是Spring In Action以及市面上流傳的大部分博客是這樣的:

  1. 實例化Bean對象,這個時候Bean的對象是非常低級的,基本不能夠被我們使用,因爲連最基本的屬性都沒有設置,可以理解爲連Autowired註解都是沒有解析的;
  2. 填充屬性,當做完這一步,Bean對象基本是完整的了,可以理解爲Autowired註解已經解析完畢,依賴注入完成了;
  3. 如果Bean實現了BeanNameAware接口,則調用setBeanName方法;
  4. 如果Bean實現了BeanClassLoaderAware接口,則調用setBeanClassLoader方法;
  5. 如果Bean實現了BeanFactoryAware接口,則調用setBeanFactory方法;
  6. 調用BeanPostProcessor的postProcessBeforeInitialization方法;
  7. 如果Bean實現了InitializingBean接口,調用afterPropertiesSet方法;
  8. 如果Bean定義了init-method方法,則調用Bean的init-method方法;
  9. 調用BeanPostProcessor的postProcessAfterInitialization方法;當進行到這一步,Bean已經被準備就緒了,一直停留在應用的上下文中,直到被銷燬;
  10. 如果應用的上下文被銷燬了,如果Bean實現了DisposableBean接口,則調用destroy方法,如果Bean定義了destory-method聲明瞭銷燬方法也會被調用。

 注意:本篇所介紹的僅僅是如何將Bean元素信息納入Spring容器的註冊表的過程,並未進行實例化。

Spring Bean的生命週期表:

Spring Bean 定義

 

2、Spring IoC 源碼時序圖(詳細版)

3、源碼追蹤

3.1詳細版鏈接   


        《SpringIOC源碼詳解


3.2 個人總結濃縮版(方便記憶)

(1)BeanFactory

         Spring Bean的創建是典型的工廠模式,這一系列的Bean工廠,也即IOC容器爲開發者管理對象間的依賴關係提供了很多便利和基礎服務,在Spring中有許多的IOC容器的實現供用戶選擇和使用,其相互關係如下:

 

其中BeanFactory作爲最頂層的一個接口類,它定義了IOC容器的基本功能規範,BeanFactory 有三個子類:ListableBeanFactory、HierarchicalBeanFactory 和AutowireCapableBeanFactory。但是從上圖中我們可以發現最終的默認實現類是 DefaultListableBeanFactory,他實現了所有的接口

那爲何要定義這麼多層次的接口呢?查閱這些接口的源碼和說明發現,每個接口都有他使用的場合,它主要是爲了區分在 Spring 內部在操作過程中對象的傳遞和轉化過程中,對對象的數據訪問所做的限制。例如

  •  ListableBeanFactory 接口表示這些 Bean 是可列表的,
  • 而 HierarchicalBeanFactory 表示的是這些 Bean 是有繼承關係的,也就是每個Bean 有可能有父 Bean。
  • AutowireCapableBeanFactory 接口定義 Bean 的自動裝配規則。這四個接口共同定義了 Bean 的集合、Bean 之間的關係、以及 Bean 行爲.

最基本的IOC容器接口BeanFactory:

public interface BeanFactory {    
     
     //對FactoryBean的轉義定義,因爲如果使用bean的名字檢索FactoryBean得到的對象是工廠生成的對象,    
     //如果需要得到工廠本身,需要轉義           
     String FACTORY_BEAN_PREFIX = "&"; 
        
     //根據bean的名字,獲取在IOC容器中得到bean實例    
     Object getBean(String name) throws BeansException;    
   
    //根據bean的名字和Class類型來得到bean實例,增加了類型安全驗證機制。    
     Object getBean(String name, Class requiredType) throws BeansException;    
    
    //提供對bean的檢索,看看是否在IOC容器有這個名字的bean    
     boolean containsBean(String name);    
    
    //根據bean名字得到bean實例,並同時判斷這個bean是不是單例    
    boolean isSingleton(String name) throws NoSuchBeanDefinitionException;    
    
    //得到bean實例的Class類型    
    Class getType(String name) throws NoSuchBeanDefinitionException;    
    
    //得到bean的別名,如果根據別名檢索,那麼其原名也會被檢索出來    
   String[] getAliases(String name);    
    
 }

在BeanFactory裏只對IOC容器的基本行爲作了定義,根本不關心你的bean是如何定義怎樣加載的。正如我們只關心工廠裏得到什麼的產品對象,至於工廠是怎麼生產這些對象的,這個基本的接口不關心。

            而要知道工廠是如何產生對象的,我們需要看具體的IOC容器實現,spring提供了許多IOC容器的實現。比如XmlBeanFactory,ClasspathXmlApplicationContext等。其中XmlBeanFactory就是針對最基本的ioc容器的實現,這個IOC容器可以讀取XML文件定義的BeanDefinition(XML文件中對bean的描述),如果說XmlBeanFactory是容器中的屌絲,ApplicationContext應該算容器中的高帥富.

            ApplicationContext是Spring提供的一個高級的IoC容器,它除了能夠提供IoC容器的基本功能外,還爲用戶提供了以下的附加服務。

從ApplicationContext接口的實現,我們看出其特點:

public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
		MessageSource, ApplicationEventPublisher, ResourcePatternResolver {


	@Nullable
	String getId();

	
	String getApplicationName();

	
	String getDisplayName();


	long getStartupDate();

	@Nullable
	ApplicationContext getParent();


	AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

}

         1.  支持信息源,可以實現國際化。(實現MessageSource接口)

         2.  訪問資源。(實現ResourcePatternResolver接口,這個後面要講)

         3.  支持應用事件。(實現ApplicationEventPublisher接口)

         4.ApplicationContext 繼承了 ListableBeanFactory,這個 Listable 的意思就是,通過這個接口,我們可以獲取多個 Bean,大家看源碼會發現,最頂層 BeanFactory 接口的方法都是獲取單個 Bean 的。

(2) BeanDefinition

         SpringIOC容器管理了我們定義的各種Bean對象及其相互的關係,Bean對象在Spring實現中是以BeanDefinition來描述的,其繼承體系如下:

 

Bean 的解析過程非常複雜,功能被分的很細,因爲這裏需要被擴展的地方很多,必須保證有足夠的靈活性,以應對可能的變化。Bean 的解析主要就是對 Spring 配置文件的解析,最後封裝成一個個的BeanDefinition對象保存在Ioc容器中(注意,這裏並沒有實例化的對象,只能認爲一個bean對象的信息,如是否懶加載,屬性引用,是否單例等等)。這個解析過程主要通過下圖中的類完成:

 

(3)各種IOC容器繼承體系

  IoC容器的初始化包括BeanDefinition的Resource定位、載入和註冊這三個基本的過程。我們以ApplicationContext爲例講解,ApplicationContext系列容器也許是我們最熟悉的,因爲web項目中使用的XmlWebApplicationContext就屬於這個繼承體系,還有ClasspathXmlApplicationContext等,其繼承體系如下圖所示:

(4)精彩部分來了

ApplicationContext允許上下文嵌套,通過保持父上下文可以維持一個上下文體系。對於bean的查找可以在這個上下文體系中發生,首先檢查當前上下文,其次是父上下文,逐級向上,這樣爲不同的Spring應用提供了一個共享的bean定義環境。

 

下面我們分別簡單地演示一下ioc容器的創建過程

—>定位

1 入口 (ClassPathXmlApplicationContext)

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

2 構造方法裏調用父類的構造方法完成資源加載器和父容器設置動作

 (AbstractApplicationContext)

public abstract class AbstractApplicationContext extends DefaultResourceLoader
		implements ConfigurableApplicationContext {
......

//獲取一個Spring Source的加載器用於讀入Spring Bean定義資源文件
	protected ResourcePatternResolver getResourcePatternResolver() {
		//AbstractApplicationContext繼承DefaultResourceLoader,因此也是一個資源加載器
		//Spring資源加載器,其getResource(String location)方法用於載入資源
		return new PathMatchingResourcePatternResolver(this);
	}

//-------------父容器賦值
public void setParent(@Nullable ApplicationContext parent) {
		this.parent = parent;
		if (parent != null) {
			Environment parentEnvironment = parent.getEnvironment();
			if (parentEnvironment instanceof ConfigurableEnvironment) {
				getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
			}
		}
	}

......
}

3 調用setConfigLocations(configLocations);將配置文件路徑保存到字符串數組

(AbstractRefreshableConfigApplicationContext)

public abstract class AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
		implements BeanNameAware, InitializingBean {
......
//解析Bean定義資源文件的路徑,處理多個資源文件字符串數組
	public void setConfigLocations(@Nullable String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
				// resolvePath爲同一個類中將字符串解析爲路徑的方法
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}
......
}

4 調用refresh()方法完成容器啓動準備 (模板方法)

 (AbstractApplicationContext)

public abstract class AbstractApplicationContext extends DefaultResourceLoader
		implements 
public void refresh() throws BeansException, IllegalStateException {
......

		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			//1、調用容器準備刷新的方法,獲取容器的當時時間,同時給容器設置同步標識
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			//2、告訴子類啓動refreshBeanFactory()方法,Bean定義資源文件的載入從
			//子類的refreshBeanFactory()方法啓動
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			//3、爲BeanFactory配置容器特性,例如類加載器、事件處理器等
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				//4、爲容器的某些子類指定特殊的BeanPost事件處理器
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				//5、調用所有註冊的BeanFactoryPostProcessor的Bean
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				//6、爲BeanFactory註冊BeanPost事件處理器.
				//BeanPostProcessor是Bean後置處理器,用於監聽容器觸發的事件
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				//7、初始化信息源,和國際化相關.
				initMessageSource();

				// Initialize event multicaster for this context.
				//8、初始化容器事件傳播器.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				//9、調用子類的某些特殊Bean初始化方法
				onRefresh();

				// Check for listener beans and register them.
				//10、爲事件傳播器註冊事件監聽器.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				//11、初始化所有剩餘的單例Bean
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				//12、初始化容器的生命週期事件處理器,併發布容器的生命週期事件
				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.
				//13、銷燬已創建的Bean
				destroyBeans();

				// Reset 'active' flag.
				//14、取消refresh操作,重置容器的同步標識。
				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...
				//15、重設公共緩存
				resetCommonCaches();
			}
		}
	}
.......
}

5 在AbstractApplicationContext的refresh()裏調用abtainFreshBeanFactory(),---->進入abtainFreshBeanFactory(),有個refreshBeanFactory()的抽象方法留給子類實現(委派模式)

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
		//這裏使用了委派設計模式,父類定義了抽象的refreshBeanFactory()方法,
        //具體實現調用子類容器的refreshBeanFactory()方法
		refreshBeanFactory();
		ConfigurableListableBeanFactory beanFactory = getBeanFactory();
		if (logger.isDebugEnabled()) {
			logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
		}
		return beanFactory;
	}

6 進入子類AbstractRefreshableApplicationContext的refreshBeanFactory(),然後調用createBeanFactory(),完成容器創建。(AbstractRefreshableApplicationContext)

public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
......

protected DefaultListableBeanFactory createBeanFactory() {
		return new DefaultListableBeanFactory(getInternalParentBeanFactory());
	}

@Override
	protected final void refreshBeanFactory() throws BeansException {
		//如果已經有容器,銷燬容器中的bean,關閉容器
		if (hasBeanFactory()) {
			destroyBeans();
			closeBeanFactory();
		}
		try {
			//創建IOC容器
			DefaultListableBeanFactory beanFactory = createBeanFactory();
			beanFactory.setSerializationId(getId());
			//配置是否允許 BeanDefinition 覆蓋、是否允許循環引用。
			customizeBeanFactory(beanFactory);
			//調用載入Bean定義的方法,主要這裏又使用了一個委派模式,
			// 在當前類中只定義了抽象的loadBeanDefinitions方法,具體的實現調用子類容器
			loadBeanDefinitions(beanFactory);
			synchronized (this.beanFactoryMonitor) {
				this.beanFactory = beanFactory;
			}
		}
		catch (IOException ex) {
			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
		}
	}
......


}

7 接着在refreshBeanFactory()裏調用loadBeanDefinitions方法,開始加載bean。 loadBeanDefinitions是抽象方法,留給子類去實現(委派模式)

—>加載

8 進入子類實現AbstractXmlApplicationContext的重寫方法loadBeanDefinitions

(AbstractXmlApplicationContext)

    a 創建bean的讀取器

// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		//創建XmlBeanDefinitionReader,即創建Bean讀取器,並通過回調
             //設置到容器中去,容  器使用該讀取器讀取Bean定義資源
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

    b 帶上上一步創建的XmlBanDefinitionReader開始加載bean

//Bean讀取器真正實現加載的方法
		loadBeanDefinitions(beanDefinitionReader);

9 進入AbstractXmlApplicationContext的重載方法loadBeanDefinitions,首先獲取Bean的Resource對象,然後XmlBeanDefinitionReader調用loadBeanDefinitions,根據Resource去加載

(AbstractXmlApplicationContext)

//Xml Bean讀取器加載Bean定義資源
	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
		//獲取Bean定義資源的定位
		Resource[] configResources = getConfigResources();
		if (configResources != null) {
			//Xml Bean讀取器調用其父類AbstractBeanDefinitionReader讀取定位
			//的Bean定義資源
			reader.loadBeanDefinitions(configResources);
		}
		//如果子類中獲取的Bean定義資源定位爲空,則獲取FileSystemXmlApplicationContext構造方法中setConfigLocations方法設置的資源
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
			//Xml Bean讀取器調用其父類AbstractBeanDefinitionReader讀取定位
			//的Bean定義資源
			reader.loadBeanDefinitions(configLocations);
		}
	}

10 進入AbstractBeanDefinitionReader 的loadBeanDefinitions方法,方法裏調用父接口的loadBeanDefinitions去根據Resource加載,不同格式的Resource去對應的子類實現(委派模式)

(AbstractBeanDefinitionReader )

public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
		Assert.notNull(resources, "Resource array must not be null");
		int counter = 0;
		for (Resource resource : resources) {
			counter += loadBeanDefinitions(resource);
		}
		return counter;
	}

11 進入對應的實現 XmlBeanDefinitionReader的loadBeanDefinitions方法

(XmlBeanDefinitionReader)

  a 將Resource轉碼處理

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
		//將讀入的XML資源進行特殊編碼處理
		return loadBeanDefinitions(new EncodedResource(resource));
	}

b 調用重載方法loadBeanDefinitions以讀流方式加載

//將資源文件轉爲InputStream的IO流
			InputStream inputStream = encodedResource.getResource().getInputStream();
			try {
				//從InputStream中得到XML的解析源
				InputSource inputSource = new InputSource(inputStream);
				if (encodedResource.getEncoding() != null) {
					inputSource.setEncoding(encodedResource.getEncoding());
				}
				//這裏是具體的讀取過程
				return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

c 調用方法doLoadBeanDefinitions真正加載,讀取配置文件轉換成Document對象

//將XML文件轉換爲DOM對象,解析過程由documentLoader實現
			Document doc = doLoadDocument(inputSource, resource);

d 調用registerBeanDefinitions方法做註冊Bean前準備,如獲取一個解析Document對象的解析器,然後調用解析器的registerBeanDefinitions方法進行Bean的註冊

//按照Spring的Bean語義要求將Bean定義資源解析並轉換爲容器內部數據結構
	public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
		//得到BeanDefinitionDocumentReader來對xml格式的BeanDefinition解析
		BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
		//獲得容器中註冊的Bean數量
		int countBefore = getRegistry().getBeanDefinitionCount();
		//解析過程入口,這裏使用了委派模式,BeanDefinitionDocumentReader只是個接口,
		//具體的解析實現過程有實現類DefaultBeanDefinitionDocumentReader完成
		documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
		//統計解析的Bean數量
		return getRegistry().getBeanDefinitionCount() - countBefore;
	}

—>註冊

12 進入具體解析註冊Bean的實現類,開始解析Document (DefautBeanDefinitionDocumentReader)

a 準備解析前的準備工作

//根據Spring DTD對Bean的定義規則解析Bean定義Document對象
	@Override
	public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
		//獲得XML描述符
		this.readerContext = readerContext;
		logger.debug("Loading bean definitions");
		//獲得Document的根元素
		Element root = doc.getDocumentElement();
		doRegisterBeanDefinitions(root);
	}

b 進入真正解析的方法doRegisterBeanDefinitions,再根據不同的解析策略調用parseBeanDefinitions進行Document對象轉換成Bean的操作

//在解析Bean定義之前,進行自定義的解析,增強解析過程的可擴展性
		preProcessXml(root);
		//從Document的根元素開始進行Bean定義的Document對象
		parseBeanDefinitions(root, this.delegate);
		//在解析Bean定義之後,進行自定義的解析,增加解析過程的可擴展性
		postProcessXml(root);

c 進入parseBeanDefinitions方法,循環調用具體解析策略方法解析每個Element

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
		//Bean定義的Document對象使用了Spring默認的XML命名空間
		if (delegate.isDefaultNamespace(root)) {
			//獲取Bean定義的Document對象根元素的所有子節點
			NodeList nl = root.getChildNodes();
			for (int i = 0; i < nl.getLength(); i++) {
				Node node = nl.item(i);
				//獲得Document節點是XML元素節點
				if (node instanceof Element) {
					Element ele = (Element) node;
					//Bean定義的Document的元素節點使用的是Spring默認的XML命名空間
					if (delegate.isDefaultNamespace(ele)) {
						//使用Spring的Bean規則解析元素節點
						parseDefaultElement(ele, delegate);
					}
					else {
						//沒有使用Spring默認的XML命名空間,則使用用戶自定義的解//析規則解析元素節點
						delegate.parseCustomElement(ele);
					}
				}
			}
		}
		else {
			//Document的根節點沒有使用Spring默認的命名空間,則使用用戶自定義的
			//解析規則解析Document根節點
			delegate.parseCustomElement(root);
		}
	}

d 進入默認的解析策略方法parseDefautElement方法,再根據具體策略去解析

//使用Spring的Bean規則解析Document元素節點
	private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
		//如果元素節點是<Import>導入元素,進行導入解析
		if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
			importBeanDefinitionResource(ele);
		}
		//如果元素節點是<Alias>別名元素,進行別名解析
		else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
			processAliasRegistration(ele);
		}
		//元素節點既不是導入元素,也不是別名元素,即普通的<Bean>元素,
		//按照Spring的Bean規則解析元素
		else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
			processBeanDefinition(ele, delegate);
		}
		else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
			// recurse
			doRegisterBeanDefinitions(ele);
		}
	}

e 進入標籤解析策略的方法 processBeanDefinition方法,然後經過封裝成BeanDefinitionHolder,最後調用BeanDefinitionReaderUtil.registerBeanDefinition向容器註冊Bean

BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
		// BeanDefinitionHolder是對BeanDefinition的封裝,即Bean定義的封裝類
		//對Document對象中<Bean>元素的解析由BeanDefinitionParserDelegate實現
		// BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
		if (bdHolder != null) {
			bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
			try {
				// Register the final decorated instance.
				//向Spring IOC容器註冊解析得到的Bean定義,這是Bean定義向IOC容器註冊的入口
				BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
			}
			catch (BeanDefinitionStoreException ex) {
				getReaderContext().error("Failed to register bean definition with name '" +
						bdHolder.getBeanName() + "'", ele, ex);
			}

 BeanDefinitonHolder類:

public class BeanDefinitionHolder implements BeanMetadataElement {

	private final BeanDefinition beanDefinition;

	private final String beanName;

	@Nullable
	private final String[] aliases;
}

13 進入BeanDefinitionReaderUtil.registerBeanDefinition方法,調用BeanDefinitionRegistry.registerBeanDefinition方法beanName和BeanDefinition爲入參向容器註冊

(BeanDefinitionReaderUtil)

//獲取解析的BeanDefinition的名稱
		String beanName = definitionHolder.getBeanName();
		//向IOC容器註冊BeanDefinition
		registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

 14 進入registerBeanDefinition的具體實現類DefaultListableBeanFactory,可以happy的看到以key=beanName和value=BeanDefinition向IOC beanDefinitionMap中put

this.beanDefinitionMap.put(beanName, beanDefinition);

4、總結

通過上面的代碼,總結一下IOC容器初始化的基本步驟:

1、初始化的入口在容器實現中的refresh()調用來完成

2、對Bean定義載入IOC容器使用的方法是loadBeanDefinition()

其中的大致流程如下:

  • (定位)通過ResourceLoader來完成資源文件位置的定位,DefaultResourceLoader是默認的實現,同時上下文就給出了ResourceLoader的實現,可以從類路徑,文件系統,URL等方式來定位資源的位置。
  • 如果是XMLBeanFactory作爲IOC容器,那麼需要爲他指定Bean定義的資源,也就是說Bean定義文件時通過抽象成Resource來被IOC容器處理的
  • (加載)容器通過BeanDefinition來完成定義信息的解析和Bean信息的註冊,往往使用的是XmlBeanDefinitionReader來解析Bean的XML定義文件--實際的處理過程是委託給BeanDefinitionParserDelegate來完成的,從而得到bean的定義信息,這些信息在Spring中使用BeanDefinition對象來表示
  • (註冊)這個名字可以讓我們想到loadBeanDefinition() , reagisterBeanDefinition() 這些相關方法。他們都是爲處理BeanDefinition服務的,容器解析得到BeanDefinition以後,需要把他在IOC容器中註冊,這由IOC實現BeanDefinitionRegistry接口來實現。
  • 註冊過程就是在IOC容器內部維護一個HashMap來保存得到的BeanDefinition的過程。這個HashMap是IOC容器持有Bean信息的場所,以後對Bean的操作都是圍繞這個Hashmap來實現的。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章