AspectJAwareAdvisorAutoProxyCreator及为Bean生成署理机缘阐明
上篇文章说了,org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator这个类是Spring提供应开拓者的AOP的焦点类,软件开发,就是AspectJAwareAdvisorAutoProxyCreator完成了【类/接口–>署理】的转换进程,首先我们看一下AspectJAwareAdvisorAutoProxyCreator的条理布局:
这里最值得留意的一点是最左下角的谁人方框,我用几句话总结一下:
基于以上的阐明,将Bean生成署理的机缘已经一目了然了:在每个Bean初始化之后,假如需要,挪用AspectJAwareAdvisorAutoProxyCreator中的postProcessBeforeInitialization为Bean生成署理。
署理工具实例化—-判定是否为<bean>生成署理
上文阐明白Bean生成署理的机缘是在每个Bean初始化之后,下面把代码定位到Bean初始化之后,先是AbstractAutowireCapableBeanFactory的initializeBean要领举办初始化:
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { 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; }
初始化之前是第16行的applyBeanPostProcessorsBeforeInitialization要领,初始化之后即29行的applyBeanPostProcessorsAfterInitialization要领:
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessAfterInitialization(result, beanName); if (result == null) { return result; } } return result; }
这里挪用每个BeanPostProcessor的postProcessBeforeInitialization要领。凭据之前的阐明,看一下AbstractAutoProxyCreator的postProcessAfterInitialization要领实现:
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (!this.earlyProxyReferences.contains(cacheKey)) { return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; }
跟一下第5行的要领wrapIfNecessary:
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { if (this.targetSourcedBeans.contains(beanName)) { return bean; } if (this.nonAdvisedBeans.contains(cacheKey)) { return bean; } if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) { this.nonAdvisedBeans.add(cacheKey); return bean; } // Create proxy if we have advice. Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { this.advisedBeans.add(cacheKey); Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); this.proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } this.nonAdvisedBeans.add(cacheKey); return bean; }
第2行~第11行是一些不需要生成署理的场景判定,这里略过。首先我们要思考的第一个问题是:哪些方针工具需要生成署理?因为设置文件内里有许多Bean,必定不能对每个Bean都生成署理,因此需要一套法则判定Bean是不是需要生成署理,这套法则就是第14行的代码getAdvicesAndAdvisorsForBean:
protected List<Advisor> findEligibleAdvisors(Class beanClass, String beanName) { List<Advisor> candidateAdvisors = findCandidateAdvisors(); List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName); extendAdvisors(eligibleAdvisors); if (!eligibleAdvisors.isEmpty()) { eligibleAdvisors = sortAdvisors(eligibleAdvisors); } return eligibleAdvisors; }
顾名思义,要领的意思是为指定class寻找符合的Advisor。