Spring框架学习[AOP通知以及编程式AOP ](一)

2014-11-24 03:05:50 · 作者: · 浏览: 0

1.处理AOP配置的通知基本步骤:

(1).获取AOP配置的通知Advice:

从上一篇博客《创建AOP代理对象并对目标对象切面拦截》对Spring中采用JDK和CGLIB两种方式创建AOP动态代理的源码分析中,我们了解到,在AOP动态代理对象的回调方法中,都需要使用以下方式获取AOP配置的通知,并将获取到的通知和目标对象、代理对象等一起封装为ReflectiveMethodInvocation对象:

[java] view plaincopyprint //获取AOP配置的通知 List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass); …… //根据获取的通知、目标对象等创建ReflectiveMethodInvocation //如果是CGLIB方式,则创建CglibMethodInvocation对象: //new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy); invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); //沿着获取的通知链,递归调用所有配置的AOP通知 retVal = invocation.proceed();

(2).创建ReflectiveMethodInvocation对象:

a.创建CglibMethodInvocation对象:

[java] view plaincopyprint //CglibMethodInvocation的构造方法 public CglibMethodInvocation(Object proxy, Object target, Method method, Object[] arguments, Class targetClass, ListinterceptorsAndDynamicMethodMatchers, MethodProxy methodProxy) { //调用父类ReflectiveMethodInvocation的构造方法 super(proxy, target, method, arguments, targetClass, interceptorsAndDynamicMethodMatchers); this.methodProxy = methodProxy; this.protectedMethod = Modifier.isProtected(method.getModifiers()); }

CglibMethodInvocation继承ReflectiveMethodInvocation类,在创建时首先调用父类的初始化方法。

b.创建ReflectiveMethodInvocation对象:

[java] view plaincopyprint //ReflectiveMethodInvocation的构造方法 protected ReflectiveMethodInvocation( Object proxy, Object target, Method method, Object[] arguments, Class targetClass, ListinterceptorsAndDynamicMethodMatchers) { this.proxy = proxy; this.target = target; this.targetClass = targetClass; this.method = BridgeMethodResolver.findBridgedMethod(method); this.arguments = arguments; //将获取到的AOP通知赋值给成员变量 this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers; }

(3).处理AOP