AnnotationAwareAspectJAutoProxyCreator【InstantiationAwareBeanPostProcessor】 的作用:
1.创建对象之前调用 调用postProcessBeforeInstantiation
判断当前bean是否在advisedBeans中(保存了所有需要增强bean)
2.isInfrastructureClass(beanClass)
判断当前bean是否是基础类型的Advice、Pointcut、Advisor、AopInfrastructureBean,
* 或者是否是切面(@Aspect)
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
@Override
protected boolean isInfrastructureClass(Class<?> beanClass) {
// Previously we setProxyTargetClass(true) in the constructor, but that has too
// broad an impact. Instead we now override isInfrastructureClass to avoid proxying
// aspects. I'm not entirely happy with that as there is no good reason not
// to advise aspects, except that it causes advice invocation to go through a
// proxy, and if the aspect implements e.g the Ordered interface it will be
// proxied by that interface and fail at runtime as the advice method is not
// defined on the interface. We could potentially relax the restriction about
// not advising aspects in the future.
return (super.isInfrastructureClass(beanClass) || this.aspectJAdvisorFactory.isAspect(beanClass));
}
3.是否需要跳过
* 1)、获取候选的增强器(切面里面的通知方法)【List<Advisor> candidateAdvisors】
* 每一个封装的通知方法的增强器是 InstantiationModelAwarePointcutAdvisor;
* 判断每一个增强器是否是 AspectJPointcutAdvisor 类型的;返回true
* 2)、永远返回false
shouldSkip(beanClass, beanName)
2.执行创建对象的后置处理器
postProcessAfterInitialization
1.return wrapIfNecessary(bean, beanName, cacheKey);//包装如果需要的情况下
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
1.1>获取当前bean的所有增强器
List<Advisor> candidateAdvisors = findCandidateAdvisors();
1.2>获取当前bean可用的增强器
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
1.3>给增强器排序
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
2.1 保存到advisedBeans中
this.advisedBeans.put(cacheKey, Boolean.TRUE);
2.2创建代理对象,spring自动决定选择jdk还是cplib代理 创建代理对象的时候 传入的增强器advised,
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
3.给容器中返回当前组件使用cglib增强了的代理对象
创建代理对象,Spring自动选择jdk动态代理还是cglib动态代理