Bean後置處理器 - BeanPostProcessor#postProcessAfterInitialization
spring在初始化之後, 還呼叫了一次 Bean 的後置處理器.
程式碼片段:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization
@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; }
同樣的, 這裡肯定也是6個後置處理器.
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { return bean; }
這裡啥也沒幹
由父類實現:
org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessAfterInitialization
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { if (!(bean instanceof BeanPostProcessor) && !isInfrastructureBean(beanName) && this.beanFactory.getBeanPostProcessorCount() < this.beanPostProcessorTargetCount) { if (logger.isInfoEnabled()) { logger.info("Bean '" + beanName + "' of type [" + bean.getClass().getName() + "] is not eligible for getting processed by all BeanPostProcessors " + "(for example: not eligible for auto-proxying)"); } } return bean; } private boolean isInfrastructureBean(@Nullable String beanName) { if (beanName != null && this.beanFactory.containsBeanDefinition(beanName)) { BeanDefinition bd = this.beanFactory.getBeanDefinition(beanName); return (bd.getRole() == RootBeanDefinition.ROLE_INFRASTRUCTURE); } return false; }
從這裡看, 並沒有做實質性的操作, 主要是記錄了日誌.
主要是一個驗證, 看看 執行過的後置處理器數量和目標數量, 是否相同.
其關鍵程式碼:
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; }
裡面啥也沒幹
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof ApplicationListener) { // potentially not detected as a listener by getBeanNamesForType retrieval Boolean flag = this.singletonNames.get(beanName); if (Boolean.TRUE.equals(flag)) { // singleton bean (top-level or inner): register on the fly this.applicationContext.addApplicationListener((ApplicationListener<?>) bean); } else if (Boolean.FALSE.equals(flag)) { if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) { // inner bean with other scope - can't reliably process events logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " + "but is not reachable for event multicasting by its containing ApplicationContext " + "because it does not have singleton scope. Only top-level listener beans are allowed " + "to be of non-singleton scope."); } this.singletonNames.remove(beanName); } } return bean; }
這裡主要是將 實現了 ApplicationListener 介面的監聽器, 加入到容器中.
@Component public class MyListener implements ApplicationListener<ApplicationEvent> { @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("MyListener : " + event); } }
都說在這一步能產生 aop 代理, 那麼從上面的程式碼, 暫時還沒有看到. 接著前面的例子getEarlyBeanReference來看
示例
@Component public class IndexA { @Autowired IndexB bbb; public IndexA() { System.out.println("IndexA constructor..."); } public void printf(){ System.out.println("indexA printf : "); System.out.println("indexB --> " + (bbb == null ? null : bbb.getClass().getName())); } } @Component public class IndexB { @Autowired IndexA aaa; public IndexB() { System.out.println("IndexB constructor..."); } public void printf(){ System.out.println("indexB printf : "); System.out.println("indexA --> " + (aaa == null ? null : aaa.getClass().getName())); } } @Component @Aspect public class Aopa { @Pointcut("execution(* com.study.elvinle.ioc.cyc.IndexA.*(..))") public void pointCutMethodA() { } @Before("pointCutMethodA()") public void beforeA() { System.out.println("before invoke indexA.*() method -- Aopa"); } @Pointcut("execution(* com.study.elvinle.ioc.cyc.IndexB.*(..))") public void pointCutMethodB() { } @Before("pointCutMethodB()") public void beforeB() { System.out.println("before invoke indexB.*() method -- Aopa"); } } @EnableAspectJAutoProxy @Configuration @ComponentScan({ "com.study.elvinle.ioc.cyc" , "com.study.elvinle.ioc.aop" }) public class StartConfig { }
測試程式碼:
public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StartConfig.class); IndexA a = ac.getBean(IndexA.class); System.out.println("spring's indexA --> " + a.getClass().getName()); a.printf(); System.out.println("======================"); IndexB b = ac.getBean(IndexB.class); System.out.println("spring's indexB --> " + b.getClass().getName()); b.printf(); }
除錯結果:
通過除錯applyBeanPostProcessorsAfterInitialization 方法看到, 此處多了一個後置處理器:
AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
方法藏的比較深, 由父類實現:
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization
@Override public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (this.earlyProxyReferences.remove(cacheKey) != bean) { return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; }
可以看到, 這裡呼叫了前面同樣的方法, 來產生代理:
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) { return bean; } if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) { return bean; } if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) { this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; } // Create proxy if we have advice. Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { this.advisedBeans.put(cacheKey, Boolean.TRUE); Object proxy = createProxy( bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); this.proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; }
結合前面那篇和這篇來看, aop 代理的產生, 並不是全在 初始化方法之後的後置處理器中產生的.
還有一部分是在SmartInstantiationAwareBeanPostProcessor#getEarlyBeanReference中產生的.
IndexA 在 getEarlyBeanReference 中產生代理之後, 在這裡會再產生一次嗎?
答案是否定的. spring 很強大, 不會犯這種低階錯誤.