Spring IOC源码深度解析
IOC的核心就是代码入口就在AbstractApplictionContext
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // 在刷新容器前进行一些准备工作,例如设置容器的激活状态,校验容器环境所必须的启动参数 prepareRefresh(); // 刷新内部的BeanFactory,获得一个新鲜的BeanFactory,这里面主要是读取XML文件,将其转换为BeanDefinition ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // 对BeanFactory进行进一步的完善,包括注册应用上下文感知以及监听器感知的BeanPostProcessor, // 先注册一些系统的环境Bean prepareBeanFactory(beanFactory); try { // 给子类在初始化BeanFactory重写修改变动beanFactory的权利 postProcessBeanFactory(beanFactory); // 调用BeanFactoryPostProcessor,可以修改beanFactory,与上面不同的是,这里是类似插件的形式,耦合度更低 invokeBeanFactoryPostProcessors(beanFactory); // 提前注册BeanPostProcessor,用于后期提供代理等功能 registerBeanPostProcessors(beanFactory); // 初始化消息源,用于国际化 initMessageSource(); // 初始化应用事件广播器,用于广播应用上下文事件 initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // 为应用事件广播器初始化监听器(ApplicationListener) registerListeners(); // 实例化并注册所有非懒加载的bean finishBeanFactoryInitialization(beanFactory); // 刷新容器后的额外工作,初始化生命周期执行器,发布容器刷新完毕的应用上下文事件 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. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; } finally { // 清除掉不再需要的缓存,节省空间 resetCommonCaches(); } } }
---分析一下上面代码中的invokeBeanFactoryPostProcessors(beanFactory)源码---开始
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { // 调用所有的BeanFactoryPostProcessors PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
我们知道BeanFactoryPostProcessor可以对已经生成好的BeanDefinition进行修改,也可以动态的添加来自第三方的BeanDefinition。比如我们熟知的Mybatis中的Mapper对象。如果我们使用Spring-Mybatis包中的注解@MapperScan则可以将我们定义的mapper对象加入到Spring容器中,便于我们在项目中注入到指定的地方。
@MapperScan("com.demo") @Configuration public class Configuration(){ } @Service public class Service{ @Autowired private DemoMapper demoMapper; }
public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) { // 存放所有已经调用过的BeanFactoryPostProcessor,避免重复执行 Set<String> processedBeans = new HashSet<String>(); // Spring默认使用的BeanFactory是DefaultListableBeanFactory,其已经实现了BeanDefinitionRegistry接口 if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; // 用于存放普通的BeanFactoryPostProcessor List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>(); // 用于存放BeanDefinitionRegistryPostProcessor List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>(); /* 执行我们自定义的beanFactoryPostProcessor,Spring提供customizeContext()方法给予我们定制化构建 ApplicationContext的机会,所以在这个方法中我们可以动态的添加自定义的BeanFactoryPostProcessor。 这里将普通的BeanFactoryPostProcessor和BeanDefinitionRegistryPostProcessor区分开分别执行。 */ for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; registryPostProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(registryPostProcessor); } else { regularPostProcessors.add(postProcessor); } } String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); // 调用实现了PriorityOrdered接口的BeanFactoryPostProcessor List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>(); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } // 按照权重排序 sortPostProcessors(beanFactory, priorityOrderedPostProcessors); registryPostProcessors.addAll(priorityOrderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry); // 调用实现了 Ordered接口的,步骤跟上面一样. postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>(); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class)); processedBeans.add(ppName); } } sortPostProcessors(beanFactory, orderedPostProcessors); registryPostProcessors.addAll(orderedPostProcessors); invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry); // 最后调用其它的BeanFactoryPostProcessor // 如果有BeanDefinitionRegistryPostProcessor被执行, 则有可能会产生新的BeanDefinitionRegistryPostProcessor,因此这边将reiterate赋值为true, 代表需要再循环查找一次 boolean reiterate = true; while (reiterate) { reiterate = false; postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false); for (String ppName : postProcessorNames) { if (!processedBeans.contains(ppName)) { BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class); registryPostProcessors.add(pp); processedBeans.add(ppName); pp.postProcessBeanDefinitionRegistry(registry); reiterate = true; } } } invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); } else { invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory); } /* 上面是通过customizeContext来动态添加BeanPostProcessor,而从这里的代码我们可以看出,只要我们写好一个 BeanFactoryPostProcessor,使用任意方法将其加入到Spring容器中,Spring也可以解析后执行。这里足以看出Spring强大的兼容性*/ String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); List<String> orderedPostProcessorNames = new ArrayList<String>(); List<String> nonOrderedPostProcessorNames = new ArrayList<String>(); for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // 跳过已经执行过的 } else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } sortPostProcessors(beanFactory, priorityOrderedPostProcessors); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } sortPostProcessors(beanFactory, orderedPostProcessors); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); // 清空Bean工厂的元数据缓存,因为在BeanFactoryPostProcessor中可能已经更改了BeanDefinition,所以这里需要清空 beanFactory.clearMetadataCache(); }
这个方法比较长,我个人觉得spring应当将其拆分成一个个小的方法,使结构更加清晰。
BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,它提供了动态注册Bean的能力。
我们最常使用的是它的实现类ConfigurationClassPostProcessor,其带有一个Bean定义读取器。这个读取器可
以读取@Configuration、@Import、@ImportResource注解标注下的所有Bean定义,并最终添加到Spring的BeanFactory中。
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware { *** private ConfigurationClassBeanDefinitionReader reader; *** }
---分析一下上面代码中的invokeBeanFactoryPostProcessors(beanFactory)源码---结束
未完待续。。。
相关推荐
itjavashuai 2020-07-28
smalllove 2020-07-27
willluckysmile 2020-06-29
TiDBPingCAP 2020-06-21
丽丽 2020-06-11
shenxiuwen 2020-06-10
willluckysmile 2020-06-10
MrFuWen 2020-06-09
yuanye0 2020-06-08
whbing 2020-05-19
yuanye0 2020-05-14
kong000dao0 2020-05-10
方志朋 2020-05-08
somyjun 2020-05-01
suixinsuoyu 2020-04-29
咻pur慢 2020-04-22
tianxiaolu 2020-04-06
吾日五省我身 2020-03-28