切面拦截过滤日志

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);//如果方法包含此注解,处理数据
        if (apiOperation != null) {
//           String apiOperationNotes =apiOperation.notes();
            String[] apiOperationTags = apiOperation.tags();
            if(apiOperationTags!=null){
                List tag_list = Arrays.stream(apiOperationTags).collect(Collectors.toList());
                if(tag_list.contains("add")||tag_list.contains("upd")||tag_list.contains("del")){
//                            handlerMethod.getBean().getClass().getName()+"."+method.getName()
                }
            }        
        }
    }

getSignature());是获取到这样的信息 :修饰符+ 包名+组件名(类名) +方法名,

String methodName = joinPoint.getSignature().getName()

获取方法上的注解

Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            xxxxxx annoObj= method.getAnnotation(xxxxxx.class);
        }

知道了方法名和类的对象,通过反射可以获取类的内部任何信息

// 切面所在类
        Object target = joinPoint.getTarget();
 
        String methodName = joinPoint.getSignature().getName();
 
        Method method = null;
        for (Method m : target.getClass().getMethods()) {
            if (m.getName().equals(methodName)) {
                method = m;
               //  xxxxxx annoObj= method.getAnnotation(xxxxxx.class);同上
                break;
            }
        }

获取方法的参数

Object[] args = joinPoint.getArgs();
 @Around(value = "execution(* com.*.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("=============================开始执行,{},{}=============================",
                proceedingJoinPoint.getTarget().getClass(),
                proceedingJoinPoint.getSignature().getName());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        //执行目标
        Object result = proceedingJoinPoint.proceed();
        stopWatch.stop();
        long lastTaskTimeMillis = stopWatch.getLastTaskTimeMillis();
        if(lastTaskTimeMillis > 3000){
            log.error("=======================执行任务结束,耗时:{}毫秒",lastTaskTimeMillis);
        }else if(lastTaskTimeMillis > 2000){
            log.warn("=======================执行任务结束,耗时:{}毫秒",lastTaskTimeMillis);
        }else {
            log.info("=======================执行任务结束,耗时:{}毫秒",lastTaskTimeMillis);
        }

        return result;
    }
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Aspect
@Component
public class DemoAspect {

    //切入点:aopdemo报下所有对象的save方法
    @Pointcut("execution(public * com.kouryoushine.aop.test.*.save*(..))")
    public void save(){

    }
    /**
     * 需要在update操作前后分别获取更新前后的值
     * @param
     * @return
     */

    @AfterReturning("save()")
    public void afterReturn(JoinPoint joinPoint) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        //1.获取切入点所在目标对象
        Object targetObj =joinPoint.getTarget();
        System.out.println(targetObj.getClass().getName());
        // 2.获取切入点方法的名字
        String methodName = joinPoint.getSignature().getName();
        System.out.println("切入方法名字:"+methodName);
        // 3. 获取方法上的注解
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
           ApiLog apiLog=  method.getAnnotation(ApiLog.class);
            System.out.println("切入方法注解的title:"+apiLog.title());
        }

        //4. 获取方法的参数
        Object[] args = joinPoint.getArgs();
        for(Object o :args){
            System.out.println("切入方法的参数:"+o);
        }


    }



}
原文地址:https://www.cnblogs.com/mingforyou/p/15074263.html