zoukankan      html  css  js  c++  java
  • aop log切面

    @Aspect:描述一个切面类,定义切面类的时候需要打上这个注解

    @Component:spring-boot配置类

    package com.*.*.tools;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.Arrays;
    
    @Aspect
    @Component
    public class LogAspect {
    
        /**
         * 功能描述: 拦截对这个包下所有方法的访问
         *
         * @param:[]
         * @return:void
         **/
        @Pointcut("execution(* com.*.*.controller.*..*(..))")
        public void loginLog() {
        }
    
        // 前置通知
        @Before("loginLog()")
        public void loginBefore(JoinPoint joinPoint) {
    
            // 我们从请求的上下文中获取request,记录请求的内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    
            HttpServletRequest request = attributes.getRequest();
    
            System.out.println("请求路径 : " + request.getRequestURL());
            System.out.println("请求方式 : " + request.getMethod());
            System.out.println("方法名 : " + joinPoint.getSignature().getName());
            System.out.println("类路径 : " + joinPoint.getSignature().getDeclaringTypeName());
            System.out.println("参数 : " + Arrays.toString(joinPoint.getArgs()));
        }
    
    //    @AfterReturning(returning = "object", pointcut = "loginLog()")
    //    public void doAfterReturning(Object object) {
    //
    //        System.out.println("方法的返回值 : " + object);
    //    }
    
        // 方法发生异常时执行该方法
        @AfterThrowing(throwing = "e",pointcut = "loginLog()")
        public void throwsExecute(JoinPoint joinPoint, Exception e) {
    
            System.err.println("方法执行异常 : " + e.getMessage());
        }
    
        // 后置通知
    //    @After("loginLog()")
    //    public void afterInform() {
    //
    //        System.out.println("后置通知结束");
    //    }
    
        // 环绕通知
        @Around("loginLog()")
        public Object surroundInform(ProceedingJoinPoint proceedingJoinPoint) {
    
            long startTime=System.currentTimeMillis();
            try {
                Object o =  proceedingJoinPoint.proceed();
                long times=System.currentTimeMillis()-startTime;
                MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
                String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
                System.out.println("【"+methodName+"】执行时间:" +times/1000+"s" );
                return o;
            } catch (Throwable e) {
                e.printStackTrace();
                return null;
            }
        }
    }
  • 相关阅读:
    PSNR计算
    Latex中的(左边有大括号的)方程组解决方案汇总
    CS (Compressive sensing, 压缩传感)
    OpenCV 2.3.0 编译错误之 file INSTALL cannot find opencv_ffmpeg230d.dll(无法找到opencv_ffmpeg230d.dll)
    Visual Studio 2008 编译程序时的 mt.exe 返回错误
    fontDialog,colorDialog,saveDialog改变字体大小,颜色,以后保存图片文件实例
    Lambda入门示例实例
    C#接口入门基础知识复习
    C#,visual studio 2010中实现最简单DragDrop实例
    抽象类实现条件参数必须和抽象类中的一样
  • 原文地址:https://www.cnblogs.com/paisen/p/10144154.html
Copyright © 2011-2022 走看看