扔进AOP后

时间:2014-09-22 13:36:57

标签: java spring spring-aop

我有一个名为" LoginException"的自定义异常。它可能会从任何类中抛出。所以我想在投掷之后做一些建议(例如,打印" Ooops")。所以我决定使用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;
        }

    }

我该怎么做?

3 个答案:

答案 0 :(得分:7)

确保抛出异常

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");
    }
}

确保 Spring允许使用@Aspect此外它能够扫描您的LogoutAdvice方面,通常我会告诉他们如何

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

@AfterThrowing更改为

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

答案 1 :(得分:1)

@AfterThrowing注释

中使用切点

因此,您的注释需要看起来像下面的

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

请参阅以下链接以获得详细说明:

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

答案 2 :(得分:0)

1)检查LoginException类,因为它应该是一个合适的异常类。

2)在catch块中抛出异常对象“e”

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)在spring配置文件中

<aop:aspectj-autoproxy/>

那就够了。