1. 程式人生 > 實用技巧 >Spring IoC 容器大概流程

Spring IoC 容器大概流程

很早就看過spring IoC容器原始碼,一直沒時間做系統的整理,現在大概整理下:

核心類關係: 
    ClassPathXmlApplicationContext extends AbstractXmlApplicationContext
    AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext
    AbstractRefreshableConfigApplicationContext extends AbstractRefreshableApplicationContext
    AbstractRefreshableApplicationContext 
extends AbstractApplicationContext AbstractApplicationContext extends DefaultResourceLoader DefaultResourceLoader implements ResourceLoader private DefaultListableBeanFactory AbstractRefreshableApplicationContext.beanFactory;

大概流程:

  1 //ClassPathXmlApplicationContext 構造
  2     public
ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { 3 super(parent); 4 //對configLocations 載入路徑的處理 呼叫了 AbstractRefreshableConfigApplicationContext.setConfigLocations 5 this.setConfigLocations(configLocations);
6 if (refresh) { 7 //這是核心 重新整理context 呼叫了AbstractApplicationContext.refresh 8 this.refresh(); 9 } 10 11 } 12 //AbstractRefreshableConfigApplicationContext.setConfigLocations 13 public void setConfigLocations(String[] locations) { 14 if (locations != null) { 15 Assert.noNullElements(locations, "Config locations must not be null"); 16 this.configLocations = new String[locations.length]; 17 18 for(int i = 0; i < locations.length; ++i) { 19 //這裡解析路徑 替換佔位符 20 this.configLocations[i] = this.resolvePath(locations[i]).trim(); 21 } 22 } else { 23 this.configLocations = null; 24 } 25 } 26 //AbstractRefreshableConfigApplicationContext.resolvePath 27 protected String resolvePath(String path) { 28 return SystemPropertyUtils.resolvePlaceholders(path); 29 } 30 31 //AbstractApplicationContext.refresh 32 public void refresh() throws BeansException, IllegalStateException { 33 synchronized(this.startupShutdownMonitor) { 34 //準備重新整理 這裡記錄了startupDate=now active=true 兩個變數 35 this.prepareRefresh(); 36 //獲取 BeanFactory 及bean的定義 37 ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory(); 38 //初始化 BeanFactory 設定一些引數 39 this.prepareBeanFactory(beanFactory); 40 41 try { 42 // 43 this.postProcessBeanFactory(beanFactory); 44 this.invokeBeanFactoryPostProcessors(beanFactory); 45 //從bean工廠中找到實現了BeanPostProcessor的bean例項化並註冊BeanPostProcessor 46 this.registerBeanPostProcessors(beanFactory); 47 //初始化 MessageSource 自定義可用messageSource作為beanName 48 this.initMessageSource(); 49 //初始化ApplicationEventMulticaster事件廣播器 50 //自定義可用applicationEventMulticaster作為beanName 51 this.initApplicationEventMulticaster(); 52 //如果有需要-留給子類實現的空方法 53 this.onRefresh(); 54 //註冊 ApplicationListener 55 this.registerListeners(); 56 //載入所有剩餘的(非延遲)單例Bean 57 this.finishBeanFactoryInitialization(beanFactory); 58 //釋出事件 59 this.finishRefresh(); 60 } catch (BeansException var4) { 61 this.destroyBeans(); 62 this.cancelRefresh(var4); 63 throw var4; 64 } 65 66 } 67 } 68 69 //AbstractApplicationContext.obtainFreshBeanFactory 70 protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { 71 //重新整理BeanFactory 主要作用是銷燬原來的 BeanFactory 並建立新的 72 this.refreshBeanFactory(); 73 //從this.beanFactory獲取當前BeanFactory 74 ConfigurableListableBeanFactory beanFactory = this.getBeanFactory(); 75 if (this.logger.isDebugEnabled()) { 76 this.logger.debug("Bean factory for " + this.getDisplayName() + ": " + beanFactory); 77 } 78 79 return beanFactory; 80 } 81 82 //AbstractRefreshableApplicationContext.refreshBeanFactory 83 protected final void refreshBeanFactory() throws BeansException { 84 if (this.hasBeanFactory()) { / 85 this.destroyBeans(); 86 this.closeBeanFactory(); 87 } 88 89 try { 90 //建立新的beanFactory 91 DefaultListableBeanFactory beanFactory = this.createBeanFactory(); 92 beanFactory.setSerializationId(this.getId()); 93 this.customizeBeanFactory(beanFactory); 94 //從xml載入bean定義 95 this.loadBeanDefinitions(beanFactory); 96 synchronized(this.beanFactoryMonitor) { 97 this.beanFactory = beanFactory; 98 } 99 } catch (IOException var4) { 100 throw new ApplicationContextException("I/O error parsing bean definition source for " + this.getDisplayName(), var4); 101 } 102 } 103 104 //AbstractApplicationContext.finishRefresh 105 protected void finishRefresh() { 106 //初始化 lifecycleProcessor 107 initLifecycleProcessor(); 108 109 //lifecycleProcessor.onRefresh 110 getLifecycleProcessor().onRefresh(); 111 112 //釋出ApplicationContext重新整理事件 113 publishEvent(new ContextRefreshedEvent(this)); 114 } 115 116 //AbstractApplicationContext.initApplicationEventMulticaster 117 //APPLICATION_EVENT_MULTICASTER_BEAN_NAME="applicationEventMulticaster" 118 protected void initApplicationEventMulticaster() { 119 ConfigurableListableBeanFactory beanFactory = getBeanFactory(); 120 //給自定義ApplicationEventMulticaster留的位置 APPLICATION_EVENT_MULTICASTER_BEAN_NAME 121 if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) { 122 this.applicationEventMulticaster = 123 beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class); 124 if (logger.isDebugEnabled()) { 125 logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]"); 126 } 127 } 128 else { 129 this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); 130 beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); 131 if (logger.isDebugEnabled()) { 132 logger.debug("Unable to locate ApplicationEventMulticaster with name '" + 133 APPLICATION_EVENT_MULTICASTER_BEAN_NAME + 134 "': using default [" + this.applicationEventMulticaster + "]"); 135 } 136 } 137 } 138 139 //AbstractApplicationContext.finishRefresh 140 protected void finishRefresh() { 141 // 初始化Context生命週期 Processer 142 initLifecycleProcessor(); 143 144 145 getLifecycleProcessor().onRefresh(); 146 147 //推送 148 publishEvent(new ContextRefreshedEvent(this)); 149 } 150 151 //AbstractApplicationContext.initLifecycleProcessor 152 //LIFECYCLE_PROCESSOR_BEAN_NAME="lifecycleProcessor" 153 protected void initLifecycleProcessor() { 154 //初始化Context生命週期 bean 155 ConfigurableListableBeanFactory beanFactory = getBeanFactory(); 156 //給LifecycleProcessor留的位置LIFECYCLE_PROCESSOR_BEAN_NAME 157 if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) { 158 this.lifecycleProcessor = 159 beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class); 160 if (logger.isDebugEnabled()) { 161 logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]"); 162 } 163 } 164 else { 165 DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor(); 166 defaultProcessor.setBeanFactory(beanFactory); 167 this.lifecycleProcessor = defaultProcessor; 168 beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor); 169 if (logger.isDebugEnabled()) { 170 logger.debug("Unable to locate LifecycleProcessor with name '" + 171 LIFECYCLE_PROCESSOR_BEAN_NAME + 172 "': using default [" + this.lifecycleProcessor + "]"); 173 } 174 } 175 }