Spring5.0原始碼學習系列之淺談BeanFactory建立
@[TOC](文章目錄)
# 部落格前言介紹 提示:在[上一章](https://smilenicky.blog.csdn.net/article/details/109118283)的學習中,我們簡單瞭解了Spring IoC容器啟動初始化的主流程,不過並沒有詳細解釋,因為程式碼比較複雜,沒有做長篇大論,所以本文接著學習BenFactory的建立過程,學習原始碼建議帶著疑問去學,一點點跟,時間積累之後就可以串起來
提示:以下是本篇文章正文內容,下面案例可供參考 # 一、獲取BeanFactory主流程 在前面的學習中,#refresh是IoC容器建立很關鍵的主線,在程式碼裡,可以找到`obtainFreshBeanFactory`這個比較關鍵的方法,這個方法就是BeanFactory的建立過程方法,本文`ClassPathXmlApplicationContext`為準,進行原始碼的學習,上篇部落格是以`AnnotationApplicationContext`為準進行簡單的分析,本文繼續比較詳細的講述 實驗環境: * SpringFramework版本 * Springframework5.0.x * 開發環境 * JAR管理:gradle 4.9/ Maven3.+ * 開發IDE:IntelliJ IDEA 2018.2.5 * JDK:jdk1.8.0_31 * Git Server:Git fro window 2.8.3 * Git Client:SmartGit18.1.5(可選) maven專案,需要加上pom配置: ```xml
* SpringBean *
* @author mazq * 修改記錄 * 修改後版本: 修改人: 修改日期: 2020/11/05 10:50 修改內容: **/ public class SpringBean implements InitializingBean { public SpringBean(){ System.out.println("SpringBean建構函式"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("SpringBean afterPropertiesSet"); } } ``` applicationContext.xml: ```xml
* TestController ** *
* @author mazq * 修改記錄 * 修改後版本: 修改人: 修改日期: 2020/11/05 10:22 修改內容: **/ public class TestApplication { public static void testClassPathXmlApplicationContext() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); SpringBean springBean = context.getBean(SpringBean.class); System.out.println(springBean); } public static void main(String[] args) { // 測試ClassPathXmlApplicationContext testClassPathXmlApplicationContext(); } } ``` 在SpringBean 打斷點,debug進行除錯:找到#refresh方法 ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/2020111114263066.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70#pic_center) obtainFreshBeanFactory示例: ```java /** * Tell the subclass to refresh the internal bean factory. * @return the fresh BeanFactory instance * @see #refreshBeanFactory() * @see #getBeanFactory() */ protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { // 建立BeanFactory refreshBeanFactory(); // 返回refreshBeanFactory方法建立好的BeanFactory ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; } ``` 然後`refreshBeanFactory`是由哪個基類實現的?我們找來一張uml類圖: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20201111142257736.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70#pic_center) 從圖可以知道`ClassPathXmlApplicationContext`通過`AbstractRefreshableApplicationContext`實現`AbstractApplicationContext`,所以`refreshBeanFactory`是`AbstractRefreshableApplicationContext#refreshBeanFactory` ok,所以主體的路線,就可以熟悉,用uml時序圖表示: ![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20201111150952587.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70#pic_center) # 二、refreshBeanFactory建立過程 `refreshBeanFactory`方法是BeanFactory的建立過程,接著跟 `org.springframework.context.support.AbstractRefreshableApplicationContext#refreshBeanFactory`程式碼如下(示例): ```java /** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { // 先判斷是否有BeanFactory if (hasBeanFactory()) { // destroy Beans destroyBeans(); // 關閉BeanFactory closeBeanFactory(); } try { // 例項化DefaultListableBeanFactory DefaultListableBeanFactory beanFactory = createBeanFactory(); // 設定序列化id beanFactory.setSerializationId(getId()); // 定義BeanFactory的一些屬性(是否允許Bean覆蓋,是否允許迴圈依賴) customizeBeanFactory(beanFactory); // 載入應用中的BeanDefinition loadBeanDefinitions(beanFactory); this.beanFactory = beanFactory; } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } } ``` `{@link org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions}` ```java /** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. // 給BeanFactory例項化一個XmlBeanDefinitionReader例項 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. // 給子類提供初始化XmlBeanDefinitionReader的模板方法 initBeanDefinitionReader(beanDefinitionReader); // 重點在這,繼續跟 loadBeanDefinitions(beanDefinitionReader); } // org.springframework.context.support.AbstractXmlApplicationContext#loadBeanDefinitions(org.springframework.beans.factory.support.DefaultListableBeanFactory) 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); } } ``` `{@link org.springframework.beans.factory.support.AbstractBeanDefinitionReader#loadBeanDefinitions}` ```java @Override public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException { Assert.notNull(resources, "Resource array must not be null"); int counter = 0; // 變數資源物件,載入BeanDefinition for (Resource resource : resources) { // 在這裡,經過debug,調到XmlBeanDefinitionReader.loadBeanDefinitions(resource) counter += loadBeanDefinitions(resource); } // 返回統計數量,表示總共有多少個BeanDefinition return counter; } ``` `XmlBeanDefinitionReader`類 `{@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader#loadBeanDefinitions}` ```java @Override public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException { return loadBeanDefinitions(new EncodedResource(resource)); } // 省略... /** * Load bean definitions from the specified XML file. * @param encodedResource the resource descriptor for the XML file, * allowing to specify an encoding to use for parsing the file * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of loading or parsing errors */ public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Assert.notNull(encodedResource, "EncodedResource must not be null"); if (logger.isInfoEnabled()) { logger.info("Loading XML bean definitions from " + encodedResource); } // 用ThreadLocal存放資源物件,目的是保證執行緒安全 Set
Creates a new instance of the parser class and invokes * {@code registerBeanDefinitions} on it. * @param doc the DOM document * @param resource the resource descriptor (for context information) * @return the number of bean definitions found * @throws BeanDefinitionStoreException in case of parsing errors * @see #loadBeanDefinitions * @see #setDocumentReaderClass * @see BeanDefinitionDocumentReader#registerBeanDefinitions */ public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException { BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader(); // 獲取已有的BeanDefinition數量 int countBefore = getRegistry().getBeanDefinitionCount(); // 註冊BeanDefinition,關注點:registerBeanDefinitions、createReaderContext documentReader.registerBeanDefinitions(doc, createReaderContext(resource)); // 返回新註冊的BeanDefinition統計數量 return getRegistry().getBeanDefinitionCount() - countBefore; } ``` `{@link org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader#registerBeanDefinitions}` ```java /** * This implementation parses bean definitions according to the "spring-beans" XSD * (or DTD, historically). *
Opens a DOM Document; then initializes the default settings
* specified at the {@code
# 知識點歸納
提示:這裡對文章進行歸納:
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20201109160646412.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ0MjczOTE=,size_16,color_FFFFFF,t_70#pic