首页

关于springframework的MethodBeforeAdvice/ThrowsAdvice方法执行前过滤、异常代码示例说明

标签:springframework,MethodBeforeAdvice,方法拦截,ThrowsAdvice,异常过滤,示例代码     发布时间:2018-09-24   

一、前言

关于springframeworkspring-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@}