一、前言
关于springframework的spring-aop包org.springframework.aop.ThrowsAdvice异常拦截、org.springframework.aop.MethodBeforeAdvice方法过滤前拦截处理,详情参见源码说明。
二、代码示例
1. MethodBeforeAdvice方法接口
import java.lang.reflect.Method;@b@import org.springframework.aop.MethodBeforeAdvice;@b@@b@public class LogAdvisor implements MethodBeforeAdvice {@b@@b@ public void before(Method m, Object[] args, Object target) throws Throwable {@b@ System.out.println("[Log] " + target.getClass().getName() + "." + m.getName() + "()");@b@ }@b@@b@}
2. ThrowsAdvice 异常接口
import org.springframework.aop.ThrowsAdvice;@b@@b@public class ExceptionAdvisor implements ThrowsAdvice {@b@ public void afterThrowing(RuntimeException re) throws Throwable {@b@ System.out.println("[Exception] " + re.getMessage());@b@ }@b@}