Spring 源码解析 Spring生命周期详解
前言:
① Spring 利用 策略模式 在生命周期中执行各种 后置处理器,每种后置处理器实现不同的功能。
② Spring 中到处用到了缓存,比如getBean() 时,会去缓存中查找,缓存中没有再去创建Bean。
Spring中最重要的三级缓存
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); //一级缓存
private final Map<String, Object> earlySingletonObjects = new HashMap<>(16); // 二级缓存
private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap<>(16); // 三级缓存
Spring 中Bean的创建过程:
第一步:扫描包
作用:获取BeanDefinition 放到map中,后续循环BeanDefinition 初始化Bean
[code]registerBeanPostProcessors(beanFactory) //完成扫描
第二步:第一次调用后置处理器 (实例化之前)
作用:判断正在初始化的Bean是否需要代理
重写实现:CommonAnnotationBeanPostProcessor 后置处理器
[code]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; }
第三步: 第二次调用后置处理器 实例化之前
作用:推断构造函数
重写实现:AutowiredAnnotationBeanPostProcessor 后置处理器
[code]protected Constructor<?>[] determineConstructorsFromBeanPostProcessors(@Nullable Class<?> beanClass, String beanName) throws BeansException { if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; Constructor<?>[] ctors = ibp.determineCandidateConstructors(beanClass, beanName); if (ctors != null) { return ctors; } } } } return null; }
第四步: 利用反射 实例化 对象
作用:实例化Bean 放入singletonFactories (三级缓存)缓存中
[code]createBeanInstance(beanName, mbd, args); ctor.newInstance(argsWithDefaultValues)
第五步: 第三次调用后置处理器 实例化之后 注入属性之前
作用:
1.合同父子BeanDefinition
2.找到该类所有需要依赖注入的点(属性和方法)
3.Merged的作用:防止后置处理器对BeanDefinition做过修改,所以需要重新合并(Spring中不停的在做合并);
原理:将BeanDefinition及父类BeanDefinition全部合并到 RootBeanDefinition 中
详细:
commonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
1.调用父类的方法,查找所有生命周期的回调方法--初始化和销毁方法 放入缓存中
2.findResourceMetadata ---找出所有需要完成注入的‘点’---@Resource @Value注解 放入缓存中
3.checkConfigMembers --injectedElements 做一个复制
AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
1.findAutowiredMetadata ---找出所有需要完成注入的‘点’---@Autowired @Value注解 放入缓存中
2.checkConfigMembers --injectedElements 做一个复制 放入缓存中
[code]protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class<?> beanType, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof MergedBeanDefinitionPostProcessor) { MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } }
第六步: 第四次调用后置处理器 实例化之后 注入属性之前 populateBean
作用:处理用户提供的后置处理器 (判断是否继续执行属性填充)
重写实现:CommonAnnotationBeanPostProcessor 后置处理器
[code]if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { return; } } } }
第七步: 第五次 调用后置处理器 注入属性 populateBean
作用:对实例化的bean 完成属性填充
重写实现:CommonAnnotationBeanPostProcessor、
AutowiredAnnotationBeanPostProcessor 后置处理器
详细:
commonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
1.findResourceMetadata ---找出所有@Resource 注解 的属性及属性值
AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition
1.findAutowiredMetadata ---找出所有@Autowired注解 的属性及属性值
[code]for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { return; } } pvs = pvsToUse; } }
第八步: 第六次调用后置处理器 (循环依赖时调用,解决循环依赖问题)
作用:利用 singletonFactories (三级缓存)工厂创建 早期的 单例 bean 存放到二级缓存中,删除该Bean的三级缓存
[code]protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) { Object exposedObject = bean; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof SmartInstantiationAwareBeanPostProcessor) { SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp; exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName); } } } return exposedObject; }
第九步:第七次 调用后置处理器
作用:调用 各种aware,调用第五步中找到的前置生命周期方法
[code]public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
第十步:第八次调用后置处理器
作用:调用第④步中找到的后置生命周期方法 (完成AOP代理)
[code]@Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor processor : getBeanPostProcessors()) { Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }
第十一步:将Bean加入到 单列池中(一级缓存中) 同时删除二级、三级缓存
[code]if (newSingleton) { addSingleton(beanName, singletonObject); }
- spring 源码解读与设计详解:6 BeanDefinitionParserDelegate与资源解析
- Spring源码解析二:IOC容器初始化过程详解
- SpringBoot之DispatcherServlet详解及源码解析
- (一)SpringBoot源码解析----启动过程refresh()方法详解
- Spring5源码解析-论Spring DispatcherServlet的生命周期
- spring boot 源码解析38-GaugeService详解
- SpringBoot内置生命周期事件详解 SpringBoot源码(十)
- SpringBoot内置生命周期事件详解 SpringBoot源码(十)
- spring boot 源码解析50-Exporter详解
- SpringBoot Profile使用详解及配置源码解析
- Spring IOC原理源码解析(@Autowired原理详解 :标识属性与方法)(二 )
- SpringBoot内置生命周期事件详解 SpringBoot源码(十)
- SpringBoot内置生命周期事件详解 SpringBoot源码(十)
- Spring IOC容器启动流程源码解析(一)——容器概念详解及源码初探
- Spring 源码解析 1.4 -- Bean的生命周期
- Spring5.0源码深度解析之SpringBean的生命周期
- spring boot 源码解析51-MetricExporters详解
- spring boot 源码解析56-actuator请求处理流程(以EnvironmentEndpoint为例)
- Spark源码系列(九)Spark SQL初体验之解析过程详解
- Spring源码解析(二)——容器刷新