Spring源码阅读(七)
2017-10-18 18:51
363 查看
这一讲主要分析bean注册过程中各种初始化方法回调的执行逻辑(initializeBean)
/** * Initialize the given bean instance, applying factory callbacks * as well as init methods and bean post processors. * 生成bean实例,并且回调诸如init methods,bean post processors方法 * <p>Called from {@link #createBean} for traditionally defined beans, * and from {@link #initializeBean} for existing bean instances. * @param beanName the bean name in the factory (for debugging purposes) * @param bean the new bean instance we may need to initialize * @param mbd the bean definition that the bean was created with * (can also be {@code null}, if given an existing bean instance) * @return the initialized bean instance (potentially wrapped) * @see BeanNameAware * @see BeanClassLoaderAware * @see BeanFactoryAware * @see #applyBeanPostProcessorsBeforeInitialization * @see #invokeInitMethods * @see #applyBeanPostProcessorsAfterInitialization */ protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareMethods(beanName, bean); return null; }, getAccessControlContext()); } else { // 执行BeanNameAware,BeanClassLoaderAware,BeanFactoryAware接口方法 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { // 执行BeanPostProcessor的postProcessBeforeInitialization方法,包括@PostConstruct注解方法 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { // 执行InitializingBean的afterPropertiesSet方法,包括init-method方法 invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { // 执行BeanPostProcessor的postProcessAfterInitialization方法 wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
相关文章推荐
- Spring源码阅读(2)- bean解析初体验
- Spring源码阅读(三)—IOC容器依赖注入
- Spring源码阅读入门指引
- Spring源码解析——如何阅读源码
- spring 源码阅读入门
- Tiny-Spring源码阅读(一)IOC的理解
- Spring源码阅读(一)
- Spring学习1:Spring源码阅读环境搭建
- 初始化IoC容器(Spring源码阅读)-我们到底能走多远系列(31)
- Spring源码阅读之准备篇-Spring整体架构分析
- spring-mvc源码阅读(一)servlet
- spring-mvc源码阅读(三)RequestMappingHandlerMapping
- Spring源码阅读4.2-Aspecjt AOP之代理对象的创建
- Spring源码阅读-实例化策略InstantiationStrategy
- spring源码阅读笔记(一)
- eclipse怎么阅读Spring源码
- Spring源码阅读 --BeanDefinition
- Spring源码阅读-DefaultListableBeanFactory的属性注释翻译
- Spring 源码阅读-BeanFactory
- Spring源码阅读——Bean的加载和获取过程