扔进AOP后

Ton*_*ony 3 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

码:

@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;
        }

    }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Man*_*dan 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;
}
Run Code Online (Sandbox Code Playgroud)

关于

@Aspect
public class LogoutAdvice {

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

确保 Spring能够使用@Aspect并且能够扫描您的LogoutAdvice方面,通常我会告诉它们如何

@Aspect
@Component// to be scanned by Spring
public class LogoutAdvice {
Run Code Online (Sandbox Code Playgroud)

改变你@AfterThrowing

@AfterThrowing(pointcut = "execution(* *.*(..))",throwing = "e")
Run Code Online (Sandbox Code Playgroud)