spring源码分析:spring生命周期二
来源:互联网 发布:苏州淘宝运营培训 编辑:程序博客网 时间:2024/06/04 20:10
接着上一篇我们看看具体是哪里的代码执行了。
1.初始化BeanFactoryPostProcessor
invokeBeanFactoryPostProcessors(beanFactory);-->PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String postProcessorName : nonOrderedPostProcessorNames) { //这里有一个getBean这里进行BeanFactoryPostProcessor的初始化操作 nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class)); }//这里执行BeanFactoryPostProcessor.postProcessBeanFactory invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
执行BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
private static void invokeBeanDefinitionRegistryPostProcessors( Collection<? extends BeanDefinitionRegistryPostProcessor> postProcessors, BeanDefinitionRegistry registry) { for (BeanDefinitionRegistryPostProcessor postProcessor : postProcessors) { postProcessor.postProcessBeanDefinitionRegistry(registry); } }
2.执行BeanFactoryPostProcessor的postProcessBeanFactory
private static void invokeBeanFactoryPostProcessors( Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) { for (BeanFactoryPostProcessor postProcessor : postProcessors) { postProcessor.postProcessBeanFactory(beanFactory); } }
3.初始化BeanPostProcessor。
registerBeanPostProcessors(beanFactory);-->protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this); } public static void registerBeanPostProcessors( ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {.... // Now, register all regular BeanPostProcessors. List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(); for (String ppName : nonOrderedPostProcessorNames) { //同样这里getBean对BeanPostProcessor进行实例化 BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); nonOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } }....}
4.执行InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
这里只以单例为例
AbstractBeanFactory.doGetBean(final String name, @Nullable final Class<T> requiredType, @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {... // Create bean instance. if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, () -> { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. destroySingleton(beanName); throw ex; } });...}进入createBeanObject bean = resolveBeforeInstantiation(beanName, mbdToUse);protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) { Object bean = null; if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) { // Make sure bean class is actually resolved at this point. if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { Class<?> targetType = determineTargetType(beanName, mbd); if (targetType != null) { bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName); if (bean != null) { bean = applyBeanPostProcessorsAfterInitialization(bean, beanName); } } } mbd.beforeInstantiationResolved = (bean != null); } return bean; } protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName); if (result != null) { return result; } } } return null; }上面已经执行了postProcessBeforeInstantiation
5.执行bean的构造函数
进入Object beanInstance = doCreateBean(beanName, mbdToUse, args);if (instanceWrapper == null) { //这里面进行初始化 instanceWrapper = createBeanInstance(beanName, mbd, args); }
6.执行
postProcessBeforeInstantiation.postProcessPropertyValues
进入populateBean(beanName, mbd, instanceWrapper);if (hasInstAwareBpps || needsDepCheck) { PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) {//这里执行postProcessBeforeInstantiation.postProcessPropertyValues pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { checkDependencies(beanName, mbd, filteredPds, pvs); } }applyPropertyValues(beanName, mbd, bw, pvs);
7.为bean注入属性
applyPropertyValues(beanName, mbd, bw, pvs);
8.执行bean实现的有关Aware接口
进入 exposedObject = initializeBean(beanName, exposedObject, mbd);//这里给Aware赋予aware接口的能力invokeAwareMethods(beanName, bean);
9.执行BeanPostProcessor.postProcessBeforeInitialization
if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
10.执行@PostConstrut初始化方法
执行CommonAnnotationBeanPostProcessor.postProcessBeforeInitialization具体为什么会调用该方法放在后面统一讲解
11.执行InitializingBean.
进入invokeInitMethods(beanName, wrappedBean, mbd);protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> { ((InitializingBean) bean).afterPropertiesSet(); return null; }, getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { //这里调用 ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (StringUtils.hasLength(initMethodName) && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } } }
12.执行xml的init-method
if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (StringUtils.hasLength(initMethodName) && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { invokeCustomInitMethod(beanName, bean, mbd); } }
13.执行BeanPostProcessor.postProcessAfterInitialization
if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); }@Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { Object current = beanProcessor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
以上的代码以AbstractApplicationContext。。refresh和AbstractBeanFactory.doGetBean为入口一步步跟均能找到。
上篇讲解了spring生命周期,这篇讲解了spring哪里具体实现。下一篇就讲解应用了,注解如何实现,例如@Configutation,@Import,@PostConstrut等。
- spring源码分析:spring生命周期二
- spring源码分析:spring生命周期
- Spring的生命周期分析
- Spring源码分析-配置文件的解析(二)
- Spring源码分析----IoC容器(二)
- Spring 初始化过程详细分析 [源码] (二)
- spring源码分析(二)- 容器基础
- Spring整合MyBatis(二)源码分析
- Spring整合MyBatis(二)源码分析
- Spring Cloud源码分析(二)Ribbon
- Spring Cloud源码分析(二)Ribbon
- Spring -- 依赖注入源码分析(二)
- Spring Cloud源码分析(二)Ribbon
- Spring Cloud源码分析(二)Ribbon
- spring 源码之二springmvc实现源码分析
- spring 源码分析 spring jdbc
- Spring 源码分析:ApplicationContext
- Spring 源码分析:ApplicationContext- -
- 移动端点击300ms延迟问题和解决
- 限量珍藏先到先得!红黑撞色vivo X20王者荣耀周年庆限量版开卖
- 迅雷玩客币正式更名为"链克" 12月14日上线实名制
- 现象级AR营销助力“口碑双十二”,蚂蚁特工在全国数万大牌商户掀起“AR捉四宝”
- 轻松解决页面小困惑(html+css)
- spring源码分析:spring生命周期二
- Eclispse 新建Maven项目记录流程
- 2017上半年盘点:走在“风口”下的中译语通
- 中译语通CEO于洋:机器翻译、大数据与人工智能的坚守不可撼动
- 人工智能时代的数据中心该怎么建?腾讯给出了自己的答案
- 前端小白--JQ瀑布流+流加载
- vue中上传文件遇到的问题
- 浙外引进阿里 AI 批改中文作文,5秒内指出多处语法错误
- 百度地图凭什么以大数据助力雄安新区城市规划建设?