SpringAOP解析
AOP解析完整过程
定位需要增强的方法(切点) + 增强内容(通知) 然后通过设置代理(创建代理)进行增强
一、切点匹配底层实现
底层切点实现调用AspectJExpressionPointcut()类进行匹配,而AspectJExpressionPointcut类实现了MethodMatcher()接口
匹配任意方法或方法上注解: 切点表达式配合pointCut.match()进行匹配
//切点表达式匹配目标方法
AspectJExpressionPointcut pt1 = new AspectJExpressionPointcut();
pt1.setExpression("execution(* bar())");
pt1.matches(T1.class.getMethod("foo"), T1.class);
pt1.matches(T1.class.getMethod("bar"), T1.class);
//切点表达式匹配目标注解
AspectJExpressionPointcut pt2 = new AspectJExpressionPointcut();
pt2.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");
pt2.matches(T1.class.getMethod("foo"), T1.class);
pt2.matches(T1.class.getMethod("bar"), T1.class);
class T1{
@Transactional
public void foo(){}
public void bar(){}
}
二、类注解、接口注解的匹配
在SpringAOP中如@Transactional注解标注位置并非总是在方法上,存在标注在类上或接口上的特殊情况;PointCut的match()是在方法上进行匹配,即需要对类注解、接口注解进行特殊处理。
类注解:表示当前类所有方法标注当前注解
接口注解:表示实现当前接口的所有类标注当前注解
@Transactional
class T2{
public void foo(){}
}
@Transactional
interface I3{
void foo();
}
class T3 implements I3{
@Override
public void foo() {}
}
针对匹配类上注解或接口上注解 使用StaticMethodMatcherPointcut() 重写matches()匹配规则
MergedAnnotations():注解合并
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY: 查找元素、所有父类以及实现的父接口的全部注解;
StaticMethodMatcherPointcut pt3 = new StaticMethodMatcherPointcut() {
@Override
public boolean matches(Method method, Class<?> targetClass) {
// 检查方法上是否加了Transactional注解
MergedAnnotations annotations = MergedAnnotations.from(method);
if(annotations.isPresent(Transactional.class))
return true;
// 检查类上是否加了Transactional注解
annotations = MergedAnnotations.from(targetClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
if (annotations.isPresent(Transactional.class))
return true;
return false;
}
};
pt3.matches(T1.class.getMethod("foo"), T1.class);
pt3.matches(T1.class.getMethod("bar"), T1.class);
pt3.matches(T2.class.getMethod("foo"), T2.class);
pt3.matches(T3.class.getMethod("foo"), T3.class);