如何捕获Spring AOP建议引发的异常

时间:2010-08-03 01:33:22

标签: java spring aop spring-aop

我有自己的异常“MyOwnException”并从我的服务类

中抛出此异常
public void service() throws MyOwnException
{
// some code 

}

现在我想在一个建议中捕获MyOwnException并重新抛出一个全新的异常

public class SimpleThrowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Method method, Object[] args, Object target,
                MyOwnException ex) throws Throwable {
        throw new Exception("new Description",ex);
    }
}

现在,我怎样才能从上述建议Exception中抓住重新抛出的SimpleThrowsAdvice

1 个答案:

答案 0 :(得分:4)

你应该使用Around建议来做到这一点。见Spring AOP AfterThrowing vs. Around Advice