java spring框架aop_Java框架篇—spring AOP 实现原理

publicObject invoke(Object proxy, Method method, Object[] args) throwsThrowable {

MethodInvocation invocation = null;

Object oldProxy = null;

boolean setProxyContext = false;

TargetSource targetSource = this.advised.targetSource;

Class targetClass = null;

Object target = null;

try {

//eqauls()方法,具目标对象未实现此方法

if (!this.equalsDefined && AopUtils.isEqualsMethod(method)){

return (equals(args[0])? Boolean.TRUE : Boolean.FALSE);

}

//hashCode()方法,具目标对象未实现此方法

if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)){

return newInteger(hashCode());

}

//Advised接口或者其父接口中定义的方法,直接反射调用,不应用通知

if (!this.advised.opaque &&method.getDeclaringClass().isInterface()

&&method.getDeclaringClass().isAssignableFrom(Advised.class)) {

// Service invocations onProxyConfig with the proxy config...

return AopUtils.invokeJoinpointUsingReflection(this.advised,method, args);

}

Object retVal = null;

if (this.advised.exposeProxy) {

// Make invocation available ifnecessary.

oldProxy = AopContext.setCurrentProxy(proxy);

setProxyContext = true;

}

//获得目标对象的类

target = targetSource.getTarget();

if (target != null) {

targetClass = target.getClass();

}

//获取可以应用到此方法上的Interceptor列表

List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,targetClass);

//如果没有可以应用到此方法的通知(Interceptor),此直接反射调用 method.invoke(target, args)

if (chain.isEmpty()) {

retVal = AopUtils.invokeJoinpointUsingReflection(target,method, args);

} else {

//创建MethodInvocation

invocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);

retVal = invocation.proceed();

}

// Massage return value if necessary.

if (retVal != null && retVal == target &&method.getReturnType().isInstance(proxy)

&&!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {

// Special case: it returned"this" and the return type of the method

// is type-compatible. Notethat we can't help if the target sets

// a reference to itself inanother returned object.

retVal = proxy;

}

return retVal;

} finally {

if (target != null && !targetSource.isStatic()) {

// Must have come fromTargetSource.

targetSource.releaseTarget(target);

}

if (setProxyContext) {

// Restore old proxy.

AopContext.setCurrentProxy(oldProxy);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值