很早就看过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 }
原文:https://www.cnblogs.com/dint/p/14117236.html