Spring注解系列十五:生命周期-BeanPostProcessor原理

喜欢ヅ旅行 2022-02-03 00:29 369阅读 0赞

1、AbstractAutowireCapableBeanFactory

  1. //在bean初始化之前给bean进行属性赋值
  2. protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
  3. PropertyValues pvs = mbd.getPropertyValues();
  4. if (bw == null) {
  5. if (!((PropertyValues)pvs).isEmpty()) {
  6. throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
  7. }
  8. } else {
  9. boolean continueWithPropertyPopulation = true;
  10. if (!mbd.isSynthetic() && this.hasInstantiationAwareBeanPostProcessors()) {
  11. Iterator var6 = this.getBeanPostProcessors().iterator();
  12. while(var6.hasNext()) {
  13. BeanPostProcessor bp = (BeanPostProcessor)var6.next();
  14. if (bp instanceof InstantiationAwareBeanPostProcessor) {
  15. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor)bp;
  16. if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
  17. continueWithPropertyPopulation = false;
  18. break;
  19. }
  20. }
  21. }
  22. }
  23. if (continueWithPropertyPopulation) {
  24. if (mbd.getResolvedAutowireMode() == 1 || mbd.getResolvedAutowireMode() == 2) {
  25. MutablePropertyValues newPvs = new MutablePropertyValues((PropertyValues)pvs);
  26. if (mbd.getResolvedAutowireMode() == 1) {
  27. this.autowireByName(beanName, mbd, bw, newPvs);
  28. }
  29. if (mbd.getResolvedAutowireMode() == 2) {
  30. this.autowireByType(beanName, mbd, bw, newPvs);
  31. }
  32. pvs = newPvs;
  33. }
  34. boolean hasInstAwareBpps = this.hasInstantiationAwareBeanPostProcessors();
  35. boolean needsDepCheck = mbd.getDependencyCheck() != 0;
  36. if (hasInstAwareBpps || needsDepCheck) {
  37. PropertyDescriptor[] filteredPds = this.filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
  38. if (hasInstAwareBpps) {
  39. Iterator var9 = this.getBeanPostProcessors().iterator();
  40. while(var9.hasNext()) {
  41. BeanPostProcessor bp = (BeanPostProcessor)var9.next();
  42. if (bp instanceof InstantiationAwareBeanPostProcessor) {
  43. InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor)bp;
  44. pvs = ibp.postProcessPropertyValues((PropertyValues)pvs, filteredPds, bw.getWrappedInstance(), beanName);
  45. if (pvs == null) {
  46. return;
  47. }
  48. }
  49. }
  50. }
  51. if (needsDepCheck) {
  52. this.checkDependencies(beanName, mbd, filteredPds, (PropertyValues)pvs);
  53. }
  54. }
  55. this.applyPropertyValues(beanName, mbd, bw, (PropertyValues)pvs);
  56. }
  57. }
  58. }
  59. //初始化bean
  60. protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
  61. if (System.getSecurityManager() != null) {
  62. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  63. public Object run() {
  64. AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
  65. return null;
  66. }
  67. }, this.getAccessControlContext());
  68. } else {
  69. this.invokeAwareMethods(beanName, bean);
  70. }
  71. Object wrappedBean = bean;
  72. if (mbd == null || !mbd.isSynthetic()) {
  73. //在初始化之前工作
  74. wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
  75. }
  76. try {
  77. //执行自定义初始化
  78. this.invokeInitMethods(beanName, wrappedBean, mbd);
  79. } catch (Throwable var6) {
  80. throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
  81. }
  82. if (mbd == null || !mbd.isSynthetic()) {
  83. //在初始化之后工作
  84. wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  85. }
  86. return wrappedBean;
  87. }
  88. //执行初始化
  89. protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
  90. boolean isInitializingBean = bean instanceof InitializingBean;
  91. if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
  92. if (this.logger.isDebugEnabled()) {
  93. this.logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
  94. }
  95. if (System.getSecurityManager() != null) {
  96. try {
  97. AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
  98. public Object run() throws Exception {
  99. ((InitializingBean)bean).afterPropertiesSet();
  100. return null;
  101. }
  102. }, this.getAccessControlContext());
  103. } catch (PrivilegedActionException var6) {
  104. throw var6.getException();
  105. }
  106. } else {
  107. ((InitializingBean)bean).afterPropertiesSet();
  108. }
  109. }
  110. if (mbd != null) {
  111. String initMethodName = mbd.getInitMethodName();
  112. if (initMethodName != null && (!isInitializingBean || !"afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) {
  113. this.invokeCustomInitMethod(beanName, bean, mbd);
  114. }
  115. }
  116. }
  117. //初始化之前
  118. public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
  119. Object result = existingBean;
  120. Iterator var4 = this.getBeanPostProcessors().iterator();
  121. //遍历得到容器中所有的BeanPostProcessor
  122. do {
  123. if (!var4.hasNext()) {
  124. return result;
  125. }
  126. BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
  127. //执行postProcessorsBeforeInitialization方法,一但返回null,跳出for循环。
  128. result = beanProcessor.postProcessBeforeInitialization(result, beanName);
  129. } while(result != null);
  130. return result;
  131. }
  132. //初始化之后
  133. public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
  134. Object result = existingBean;
  135. Iterator var4 = this.getBeanPostProcessors().iterator();
  136. do {
  137. if (!var4.hasNext()) {
  138. return result;
  139. }
  140. BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
  141. result = beanProcessor.postProcessAfterInitialization(result, beanName);
  142. } while(result != null);
  143. return result;
  144. }

发表评论

表情:
评论列表 (有 0 条评论,369人围观)

还没有评论,来说两句吧...

相关阅读