2014-09-22 132 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)。