您的位置:首页 > 编程语言 > Java开发

Spring AOP的bug - @Around和@AfterThrowing不兼容

2011-05-13 16:57 267 查看
Spring2.5.6版本的测试结论:@Around和@AfterThrowing兼容,他们分别能与其他advice兼容

@Aspect
@SuppressWarnings("unused")
public class AdviceHandler {

@Pointcut("execution(* com.alibaba.normandy.service.UserManager.add*(..)))")
private void pointcutService() {
};

@Pointcut("execution(* del*(..))")
private void allAddMethod1() {
};
@Around("pointcutService()")
public void aroundServiceActions(ProceedingJoinPoint pjp) {
// before action
System.out.println("before aroundServiceActions invoked!");

try {
pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}

// after action
System.out.println("after aroundServiceActions invoked!");
}
//抛出异常后执行
@AfterThrowing("pointcutService()")
public void exceptionServiceActions() {
System.out.println("exception happened!  exceptionServiceActions invoked!");
}
}

测试结果:共用时,@Around切入点中的内容执行,而@AfterThrowing切入点里面的内容没有执行

before aroundServiceActions invoked!
java.lang.RuntimeException: throw exception e
at com.alibaba.normandy.service.UserManagerImpl.addUser(UserManagerImpl.java:7)
.......
after aroundServiceActions invoked!

后来在spring的官网论坛上面找到一篇资料印证了自己的实验,请参考:http://forum.springsource.org/showthread.php?76794-aop-after-throwing-not-working
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息