AOP源码分析--增强器查找
2019-10-18 本文已影响0人
悟空嘿
*PonintCut切点查找,处理
// org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#getAdvisor
@Override
public Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory,
int declarationOrderInAspect, String aspectName) {
validate(aspectInstanceFactory.getAspectMetadata().getAspectClass());
// 获取添加了@PointCut注解的方法,得到切点表达式
AspectJExpressionPointcut expressionPointcut = getPointcut(
candidateAdviceMethod, aspectInstanceFactory.getAspectMetadata().getAspectClass());
if (expressionPointcut == null) {
return null;
}
return new InstantiationModelAwarePointcutAdvisorImpl(expressionPointcut, candidateAdviceMethod,
this, aspectInstanceFactory, declarationOrderInAspect, aspectName);
}
根据pointcut的表达式,解析真正的切点。包括:方法,|,& 等的解析。由checkReadyToMatch方法可以知道,增强器表达式是懒加载的,在调用 findAdvisorsThatCanApply(..)的时候,才会解析ecpression字符串,得到真正的切点。
// org.springframework.aop.aspectj.AspectJExpressionPointcut#checkReadyToMatch
// 检查此切入点是否已准备好匹配,从而延迟构建基础的AspectJ切入点表达式。
private void checkReadyToMatch() {
if (getExpression() == null) {
throw new IllegalStateException("Must set property 'expression' before attempting to match");
}
if (this.pointcutExpression == null) {
this.pointcutClassLoader = determinePointcutClassLoader();
this.pointcutExpression = buildPointcutExpression(this.pointcutClassLoader);
}
}
/**
*@see org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#findEligibleAdvisors
*/
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
//查找 Advisor 增强器,在切面上添加对应的增强操作
List<Advisor> candidateAdvisors = findCandidateAdvisors();
//在所有的增强器中 查找指定bean适配的增强器
List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
extendAdvisors(eligibleAdvisors);
if (!eligibleAdvisors.isEmpty()) {
eligibleAdvisors = sortAdvisors(eligibleAdvisors);
}
return eligibleAdvisors;
}