首页 > 编程语言 > 详细

spring aop

时间:2020-11-26 15:44:55      阅读:27      评论:0      收藏:0      [点我收藏+]
advice
各种advice被封装到Interceptor中:
如AfterReturningAdviceInterceptor类持有AfterReturningAdvice的引用
public class AfterReturningAdviceInterceptor implements MethodInterceptor, AfterAdvice, Serializable {

private final AfterReturningAdvice advice;


/**
* Create a new AfterReturningAdviceInterceptor for the given advice.
* @param advice the AfterReturningAdvice to wrap
*/
public AfterReturningAdviceInterceptor(AfterReturningAdvice advice) {
Assert.notNull(advice, "Advice must not be null");
this.advice = advice;
}

//after 目标方法调用完成后执行通知器方法
//before 与 after相反
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
Object retVal = mi.proceed();
this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
return retVal;
}

}
 
 
ReflectiveMethodInvocation类中递归调用所有interceptor执行invoke()操作
public Object proceed() throws Throwable {
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();//执行目标方法
}

Object interceptorOrInterceptionAdvice =
this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
// Evaluate dynamic method matcher here: static part will already have
// been evaluated and found to match.
InterceptorAndDynamicMethodMatcher dm =
(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);//遍历所有拦截器并进行匹配,匹配到则调用通知类进行增强
}
else {
// Dynamic matching failed.
// Skip this interceptor and invoke the next in the chain.
return proceed();//否则继续匹配下一个拦截器
}
}
else {
// It‘s an interceptor, so we just invoke it: The pointcut will have
// been evaluated statically before this object was constructed.
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
 
PointCut
public final MethodMatcher getMethodMatcher()
返回一个MethodMatcher决定advice通知应该选用哪个连接点
 
Advisor通知器
将advice与pointCut结合起来
具体实现参考
public class DefaultPointcutAdvisor extends AbstractGenericPointcutAdvisor implements Serializable
内部持有pointcut与advice
 
AOP核心:动态代理
通过两种方式:
1.java proxy 对接口
2.cglib 对类
 
实现类ProxyFactoryBean
技术分享图片
 
配置proxyInterface、interceptorNames(advisor)、target(增强对象)等
获取aopProxy:
getObject()->初始化拦截器链advisor(配置读取)->newPrototypeInstance()->getProxy()
getProxy()通过ProxyCreatorSupport的AopProxyFactory(默认DefaultAopProxyFactory)创建
 
 
public class AdvisedSupport extends ProxyConfig implements Advised
持有拦截器链advisor、target等
 
DefaultAopProxyFactory创建AopProxy的策略:
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {

@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface()) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
....
}
不同的AopProxy实现有不同的回调入口
jdk是通过实现InvockationHandler的invoke()方法实现
cglib通过callback回调

spring aop

原文:https://www.cnblogs.com/fatFatCat/p/14042330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!