2015-10-15 119 views
1

當我的系統拋出一個異常時,應該調用下面的方法,但事實並非如此。它只有當我刪除註釋的 '投擲' 和 '例外' 作爲參數的工作原理:Spring AOP - @AfterThrowing

不起作用:

@AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))", throwing="e") 
public void afterThrowing(Exception e) { 
    System.out.println("Test"); 
} 

作品:

@AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))") 
public void afterThrowing() { 
    System.out.println("Test"); 
} 

有誰知道我做錯了什麼?

這是整個類:

package br.com.ogfi.portalbackoffice.aspect; 

import org.aspectj.lang.annotation.AfterThrowing; 
import org.aspectj.lang.annotation.Aspect; 
import org.springframework.stereotype.Component; 

@Aspect 
@Component 
public class AfterThrowAdvice { 


    @AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))", throwing="e") 
    public void afterThrowing(Exception e) { 
     System.out.println("Boo! We want our money back!"); 
     //ex.printStackTrace(); 
     //System.out.println("Boo! We want our money back!"); 

    } 

} 

回答

1

Finaly我發現它不工作的原因:

Java項目我與呼籲他SystemException的自己的異常工作時,同名的javax.transaction.SystemException,我還沒有意識到它不是來自javax。

我的項目中的SystemException擴展了Throwable,當我嘗試使用Exception作爲參數時,我的方法未被調用,因爲它不是同一件事。