2014-09-22 130 views
1

我有一個自定義異常,稱爲「LoginException」。它可能會從任何階級拋出。所以我想提出一個建議,在投擲之後做一些事情(例如,打印「Ooops」)。所以我決定使用AOP。事情是這樣的:投擲後AOP

@Aspect 
public class LogoutAdvice { 

    @AfterThrowing(throwing = "e") 
    public void myAfterThrowing(JoinPoint joinPoint, LoginException e) {  
     System.out.println("IDS HABBENING"); 
    } 
} 

代碼:

@Transactional 
    public DynamicTable getTable(int status_id, HttpServletRequest request) 
      throws HibernateException, LoginException, SQLException { 
     try { 
      ResultSet rs = requestDAO.getRequestResultSet(
        cookieDAO.get(SESS_ATTR, request), status_id); 
      DynamicTable dt = new DynamicTable(); 
      String[] columnArray; 
      LinkedList<String[]> dataList = new LinkedList<String[]>(); 
      ResultSetMetaData rsmd = rs.getMetaData(); 
      int columnCount = rsmd.getColumnCount(); 
      columnArray = new String[columnCount - META_COLUMNS_COUNT]; 
      for (int i = 0; i < columnArray.length; i++) { 
       columnArray[i] = rsmd.getColumnName(META_COLUMNS_COUNT + i + 1); 
      } 
      dt.setTitleArray(columnArray); 

      while (rs.next()) { 

       String[] dataArray = new String[columnArray.length]; 
       for (int i = 0; i < columnArray.length; i++) { 
        dataArray[i] = ParamUtil.toString(rs 
          .getObject(META_COLUMNS_COUNT + i + 1)); 
       } 

       dataList.add(dataArray); 

      } 
      dt.setDataList(dataList); 

      return dt; 
     } catch (SQLException e) { 
      String message = e.getMessage(); 
      String[] errorsArray = AuthErrorsConst.ERROR; 
      for (int i = 0; i < errorsArray.length; i++) { 
       if (message.contains(errorsArray[i])) { 
        throw new LoginException(); // LOOK AT THIS 

       } 
      } 
      throw e; 
     } 

    } 

我怎麼能這樣做?

+1

你在做什麼有錯誤?如果是這樣,請分享不按預期行事的細節。 – 2014-09-22 13:48:04

回答

5

確定例外是bei NG拋出

catch (SQLException e) { 
     String message = e.getMessage(); 
     String[] errorsArray = AuthErrorsConst.ERROR; 
     for (int i = 0; i < errorsArray.length; i++) { 
      if (message.contains(errorsArray[i])) { 
       System.out.println("throwing LoginException")// NEW 
       throw new LoginException(); // LOOK AT THIS 
      } 
     } 
     throw e; 
} 

關於

@Aspect 
public class LogoutAdvice { 

    @AfterThrowing(throwing = "e") 
    public void myAfterThrowing(JoinPoint joinPoint, LoginException e) {  
     System.out.println("IDS HABBENING"); 
    } 
} 

一定春天已經實現與@Aspect工作進一步它能夠掃描你的LogoutAdvice方面,通常我宣佈他們如何

@Aspect 
@Component// to be scanned by Spring 
public class LogoutAdvice { 

將您的@AfterThrowing更改爲

@AfterThrowing(pointcut = "execution(* *.*(..))",throwing = "e") 
1

使用切入點在@AfterThrowing註釋

因此您的註釋就需要類似於下面

@AfterThrowing(pointcut = "execution(public * *(..))",throwing = "e")

請參考以下鏈接精細解釋:

http://www.compiletimeerror.com/2013/05/spring-aop-after-throwing-advice.html#.VCAnsvmSw2c

+0

我應該使用bean LogoutAdvice嗎? – Tony 2014-09-22 14:07:02

+0

你的意思是爲LogoutAdvice創建bean嗎? – 2014-09-22 14:08:43

+0

是............. – Tony 2014-09-22 14:09:42

0

1)請檢查您的LoginException類,因爲它應該是一個適當的異常類。

2)在彈簧配置文件

<aop:aspectj-autoproxy/> 

這就是足夠拋出異常對象的 「e」 在catch塊

3)

@Aspect 
@Component 
public class LogoutAdvice { 
    @AfterThrowing(pointcut = "execution (* * com..*(..)", throwing = "e") 
    public void myAfterThrowing(JoinPoint joinPoint, LoginException e) {  
     System.out.println("IDS HABBENING"); 
    } 
} 

4)。