【Spring源码解析】生命周期-BeanPostProcessor在spring底层的应用
    
  
      
      
     
    
      
         前言
BeanPostProcessor 是spring定义的后置处理器, 分别会在bean初始化的前后调用。以下是调用时机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {         if (System.getSecurityManager() != null) {             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {                 invokeAwareMethods(beanName, bean);                 return null;             }, getAccessControlContext());         }         else {             invokeAwareMethods(beanName, bean);         }
          Object wrappedBean = bean;         if (mbd == null || !mbd.isSynthetic()) {             wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);         }
          try {             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()) {             wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);         }
          return wrappedBean;     }
  | 
 
 源码分析
我们以 ApplicationContextAwareProcessor 分析, ApplicationContextAwareProcessor 实现了 BeanPostProcessor 接口。
当实现了 BeanPostProcessor 接口的bean被注册时, 会走以下方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   | @Override     @Nullable     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {         if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||                 bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||                 bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||                 bean instanceof ApplicationStartupAware)) {             return bean;         }
          AccessControlContext acc = null;
          if (System.getSecurityManager() != null) {             acc = this.applicationContext.getBeanFactory().getAccessControlContext();         }
          if (acc != null) {             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {                 invokeAwareInterfaces(bean);                 return null;             }, acc);         }         else {             invokeAwareInterfaces(bean);         }
          return bean;     }
   | 
 
由源码可以, 当bean实现了 Aware 接口时, 会走 invokeAwareInterfaces(bean); 方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | private void invokeAwareInterfaces(Object bean) {     if (bean instanceof EnvironmentAware) {         ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());     }     if (bean instanceof EmbeddedValueResolverAware) {         ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);     }     if (bean instanceof ResourceLoaderAware) {         ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);     }     if (bean instanceof ApplicationEventPublisherAware) {         ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);     }     if (bean instanceof MessageSourceAware) {         ((MessageSourceAware) bean).setMessageSource(this.applicationContext);     }     if (bean instanceof ApplicationStartupAware) {         ((ApplicationStartupAware) bean).setApplicationStartup(this.applicationContext.getApplicationStartup());     }     if (bean instanceof ApplicationContextAware) {         ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);     } }
  | 
 
在spring中, Aware 接口是回调接口, 也就是说当我们需要容器中的某些bean时, 可以通过实现 Aware 接口来得到spring内置的一些bean。
 最后
本篇到此结束,欢迎大家关注公众号【当我遇上你】。