Tony
Tony

Reputation: 3805

After throwing in AOP

I've got a custom exception called "LoginException". It might be thrown from any class. So I want to make an advice to do something(for example, print "Ooops") after throwing. So I decided to use AOP. Something like this:

@Aspect
public class LogoutAdvice {

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

Code:

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

    }

How can I do that?

Upvotes: 3

Views: 10033

Answers (3)

Bitan Biswas
Bitan Biswas

Reputation: 51

1) Check you LoginException class as it should be a proper exception class.

2) throw the exception object "e" in catch block

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) in spring configuration file

<aop:aspectj-autoproxy/>

thats enough.

Upvotes: 0

Manuel Jordan
Manuel Jordan

Reputation: 16271

Be sure the exception is being thrown

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

About

@Aspect
public class LogoutAdvice {

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

Be sure Spring has enable to work with @Aspect and furthermore it be able to scan your LogoutAdvice aspect, normally I declare them how

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

Change your @AfterThrowing to

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

Upvotes: 8

Mangesh Jogade
Mangesh Jogade

Reputation: 169

Use a point cut in your @AfterThrowing annotation

Therefore your annotation will need to look something like below

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

Please refer to below link for elaborate explanation:

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

Upvotes: 1

Related Questions