zoukankan      html  css  js  c++  java
  • 寻找写代码感觉(十)之SpringBootAOP的使用

    写在前面

    之前有写过关于spring boot Aop使用之类的文章,传送门,感兴趣的同学可以,详细查看文章。

    这里其实还是为了方便调试,及日志输出使用。

    使用实例

    示例代码如下:

    package com.rongrong.wiki.aspect;
    
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.support.spring.PropertyPreFilters;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    
    @Aspect
    @Component
    public class LogAspect {
    
        private final static Logger LOG = LoggerFactory.getLogger(LogAspect.class);
    
        /** 定义一个切点 */
        @Pointcut("execution(public * com.rongrong.*.controller..*Controller.*(..))")
        public void controllerPointcut() {}
    
    
        @Before("controllerPointcut()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            // 开始打印请求日志
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            Signature signature = joinPoint.getSignature();
            String name = signature.getName();
            // 打印请求信息
            LOG.info("------------- 开始 -------------");
            LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
            LOG.info("类名方法: {}.{}", signature.getDeclaringTypeName(), name);
            LOG.info("远程地址: {}", request.getRemoteAddr());
            // 打印请求参数
            Object[] args = joinPoint.getArgs();
    		// LOG.info("请求参数: {}", JSONObject.toJSONString(args));
    		Object[] arguments  = new Object[args.length];
            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof ServletRequest
                        || args[i] instanceof ServletResponse
                        || args[i] instanceof MultipartFile) {
                    continue;
                }
                arguments[i] = args[i];
            }
            // 排除字段,敏感字段或太长的字段不显示
            String[] excludeProperties = {"password", "file"};
            PropertyPreFilters filters = new PropertyPreFilters();
            PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
            excludefilter.addExcludes(excludeProperties);
            LOG.info("请求参数: {}", JSONObject.toJSONString(arguments, excludefilter));
        }
    
        @Around("controllerPointcut()")
        public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            long startTime = System.currentTimeMillis();
            Object result = proceedingJoinPoint.proceed();
            // 排除字段,敏感字段或太长的字段不显示
            String[] excludeProperties = {"password", "file"};
            PropertyPreFilters filters = new PropertyPreFilters();
            PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
            excludefilter.addExcludes(excludeProperties);
            LOG.info("返回结果: {}", JSONObject.toJSONString(result, excludefilter));
            LOG.info("------------- 结束 耗时:{} ms -------------", System.currentTimeMillis() - startTime);
            return result;
        }
    
    
    }
    
    

    为了查看效果,避免混淆影响,需要先注释掉拦截器及过滤器部分相关代码

    重新编译,启动,查看结果如下:

    到此,SpringBootAOP的使用分享完毕

    优秀不够,你是否无可替代

    软件测试交流QQ群:721256703,期待你的加入!!

    欢迎关注我的微信公众号:软件测试君


  • 相关阅读:
    MySQL No Install zip安装方法
    xaml中绑定单例属性
    wpf 绑定ObservableCollection 的Count属性
    ItemsControl 使用Grid布局
    C# 读取oracle 中文乱码的解决方案
    wpf datagrid 行双击事件
    wpf鼠标捕获与控件交互——UIElement.CaptureMouse
    Mysql备份还原
    删除_desktop.ini病毒文件
    揭露【誉思云】打码软件的诈骗骗局!誉思云是骗人的,不要再浪费钱财!
  • 原文地址:https://www.cnblogs.com/longronglang/p/15471341.html
Copyright © 2011-2022 走看看