`
liyixing1
  • 浏览: 939490 次
  • 性别: Icon_minigender_1
  • 来自: 江西上饶
社区版块
存档分类
最新评论

Couldn't access current invocation

 
阅读更多
做了一个aop的日志记录器,但是在运行的时候出现了日志信息
服务器运行时间:577375 线程名:[http-8080-2]
日志位置:org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:292)
记录器logger名称:org.springframework.aop.aspectj.AspectJExpressionPointcut
日志等级:DEBUG
发生时间:2011-10-20 20:37:04
日志内容:Couldn't access current invocation - matching with limited context: java.lang.IllegalStateException: No MethodInvocation found: Check that an AOP invocation is in progress, and that the ExposeInvocationInterceptor is in the interceptor chain.
看起来还是调试级别的。不晓得具体原因。去查看源代码

public boolean matches(Method method, Class targetClass, Object[] args) {
checkReadyToMatch();
ShadowMatch shadowMatch = getShadowMatch(AopUtils.getMostSpecificMethod(method, targetClass), method);
ShadowMatch originalShadowMatch = getShadowMatch(method, method);

// Bind Spring AOP proxy to AspectJ "this" and Spring AOP target to AspectJ target,
// consistent with return of MethodInvocationProceedingJoinPoint
ProxyMethodInvocation pmi = null;
Object targetObject = null;
Object thisObject = null;
try {
//这里抛出的异常。ExposeInvocationInterceptor.currentInvocation();方法是从当前线程取出已经运行中的代理aop调用者。如果不存在,就抛出异常。这里不晓得spring为何设计为异常,而不是return null
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
targetObject = mi.getThis();
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
pmi = (ProxyMethodInvocation) mi;
thisObject = pmi.getProxy();
}
catch (IllegalStateException ex) {
//从这里的解释大概能看出来如果不存在调用实例,可能就会去生成一个新的实例。
// No current invocation...
// TODO: Should we really proceed here?
logger.debug("Couldn't access current invocation - matching with limited context: " + ex);
}

JoinPointMatch joinPointMatch = shadowMatch.matchesJoinPoint(thisObject, targetObject, args);

/*
* Do a final check to see if any this(TYPE) kind of residue match. For
* this purpose, we use the original method's (proxy method's) shadow to
* ensure that 'this' is correctly checked against. Without this check,
* we get incorrect match on this(TYPE) where TYPE matches the target
* type but not 'this' (as would be the case of JDK dynamic proxies).
* <p>See SPR-2979 for the original bug.
*/
//存在实例就直接使用这个实例来验证。
if (pmi != null) {  // there is a current invocation
RuntimeTestWalker originalMethodResidueTest = new RuntimeTestWalker(originalShadowMatch);
if (!originalMethodResidueTest.testThisInstanceOfResidue(thisObject.getClass())) {
return false;
}
}


if (joinPointMatch.matches() && pmi != null) {
bindParameters(pmi, joinPointMatch);
}

//不存在就return joinPointMatch.matches();这个是aspect的实现。用来查找匹配的。如果有匹配的aop方法,就return true。当return true,spring就会调用组合后的处理方法,也就是通过org.aopalliance.intercept.MethodInterceptor来调用,这里的MethodInterceptor就是代理类中加入的新的代码。如果不存在就直接return proceed()了。
return joinPointMatch.matches();
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics