After Advise

aop-after-advice-header-img1

(1) Overview & (2) Configure
(3) Example

In APO, some process called AFTER the specified point (Point cut). There are three AFTER cases:

  • Just After something (always)
  • Only after error (Exception)
  • Only after no error

The typical example is a LOGGING. This example also does logging. The logging method called AFTER the point cut (After Advise).

To implements After Advise, AspectJ provides @After, @AfterThrowing and @AfterReturning annotation:

In the line 18:
The first example will be execute AFTER “Exception” occurs in GenHbDaoImpl class:
aop-after-error-example2
3.1. The first asterisk specified ANY return type
3.2. The second asterisk specified ANY method (name) in this class
3.3. The two period “..” specified ANY number and type of method parameter

In the line 25: The second example, the only different from the first example is that it will be execute after “NO Exception” case:
aop-after-no-error-example2
3.4.@AfterReturning” means after NO ERROR

In the line 32: The third example is executed always “AFTER” something.
aop-after-example2
3.5. In this case, ANY method in “RmaController” class starts with “backTo

package com.ns.spring.aop.advise;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectAfter {

	private static final Logger log = LoggerFactory.getLogger(AspectAfter.class);

	@AfterThrowing(pointcut = "execution(* com.ns.spring.dao.GenHbDaoImpl.*(..))", throwing = "error")
	public Object afterException(JoinPoint joinPoint, Throwable error) {
		log.info("(At afterException) Method name: " + joinPoint.getSignature().getName());
		log.info("(At afterException) Exception : " + error);
		return error;
	}

	@AfterReturning(pointcut = "execution(* com.ns.spring.dao.GenHbDaoImpl.*(..))", returning = "result")
	public Object afterNormalEnd(JoinPoint joinPoint, Object result) {
		log.info("(At afterNormalEnd) Method name: " + joinPoint.getSignature().getName());
		log.info("(At afterNormalEnd) Result: " + result);
		return result;
	}
	
	@After("execution(* com.ns.spring.RmaController.backTo*(..))")
	public void beforeAllRmaCtrlGoTo(JoinPoint pt){
		log.info("\n++++++++++++++++++++ Controller (Back To screen Transition) ++++++++++++++++++++ " +pt.getSignature().getName() + " called!");
		setLogMsg(pt);
	}
	
	private void setLogMsg(JoinPoint jpt) {

		StringBuffer sb = new StringBuffer();
		sb.append("\n Class: ");
		sb.append(jpt.getTarget().getClass().getName());
		sb.append("\n Method: ");
		sb.append(jpt.getSignature().getName());
		Object[] arr = jpt.getArgs();
		if (arr != null && arr.length > 0) {
			sb.append("\n Param(s): ");
			for (Object element : arr) {
				if (element != null) {
					sb.append(element).append(",");
				}
			}
			// Delete the last comma
			sb.deleteCharAt(sb.length() - 1);
		}
		log.info(sb.toString());
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.