• springboot aop示例


    package com.melon.aop;
    
    import com.melon.utils.GSON;
    import lombok.extern.log4j.Log4j;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    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 javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Created by zhiqi.shao on 2017/5/19.
     */
    @Log4j
    @Aspect
    @Component
    public class WebLogAspect {
    
        ThreadLocal<Long> startTime = new ThreadLocal<>();
    
        @Pointcut("execution(public * com.melon.web..*.*(..))")
        public void webLog(){}
    
        @Before("webLog()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            log.info("*****request url:" + request.getRequestURL());
    
            StringBuilder sb = new StringBuilder();
            Object[] os = joinPoint.getArgs();
            for (Object o : os) {
                if (o instanceof HttpSession ||o instanceof HttpServletResponse || o instanceof HttpServletRequest|| o instanceof  Exception) {
                    continue;
                }
                sb.append(GSON.toJson(o) + ":");
            }
            log.info("*****request parameters:" + sb.toString());
    
            startTime.set(System.currentTimeMillis());
    
            // 省略日志记录内容
        }
    
        @AfterReturning(returning = "ret", pointcut = "webLog()")
        public void doAfterReturning(Object ret) throws Throwable {
            // 处理完请求,返回内容
            log.info("*****response contents : " + GSON.toJson(ret));
            log.info("*****response time : " + (System.currentTimeMillis() - startTime.get()) + "ms");
        }
    
    
    }
    

      

  • 相关阅读:
    BootStrap .row-cols 类的用法
    苹果手机浏览器$(document).on(“click”,function(){})点击无效的问题
    $("节点名").html("字符串")和$("节点名").text("字符串")区别
    linux 安装Nginx
    linux安装nginx
    vue dev开发环境跨域和build生产环境跨域问题解决
    正在载入中......loading页面的几种方法
    浏览器断点调试js
    vue组件之间传值方式解析
    基于Vue + Vuex + Vue-router + Webpack 2.0打造微信界面
  • 原文地址:https://www.cnblogs.com/shaozhiqi/p/8846878.html
Copyright © 2020-2023  润新知