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 很强大, 不会犯这种低级错误.