【Spring】SpringAop给所有Service增加日志

@Component
@Aspect
@Slf4j
public class ServiceLogAop {

    @Pointcut("within(@org.springframework.stereotype.Service *)")
    public void serviceMethods() {
    }

    @Before(value = "serviceMethods()")
    public void logMethodEntry(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        if (args.length != 0) {
            try {
                log.info("========ServiceLog==Param======: " + className + "." + methodName + " with arguments: " + JSON.toJSONString(args));
            } catch (Exception e) {
                log.info("========ServiceLog==Param======: " + className + "." + methodName + " with arguments: " + Arrays.toString(args));
            }
        }
    }

    @AfterReturning(value = "serviceMethods()", returning = "result")
    public void logMethodExit(JoinPoint joinPoint, Object result) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        if (args.length != 0) {
            try {
                log.info("========ServiceLog==result======: " + className + "." + methodName + " with result: " + JSON.toJSONString(result));
            } catch (Exception e) {
                log.info("========ServiceLog==result======: error");
            }
        }
    }
}

其他的如controller同理

@Pointcut("within(@org.springframework.stereotype.Service *) || within(@org.springframework.stereotype.Controller *)")

还可以排除

@Pointcut("within(@org.springframework.stereotype.Service *) && !execution(* springfox.documentation.schema.property.*.*(..))")

相关推荐

  1. 【Spring】SpringAop所有Service增加

    2023-12-15 19:22:03       41 阅读
  2. SpringBoot 如何快速过滤出一次请求的所有

    2023-12-15 19:22:03       19 阅读
  3. 日常开发

    2023-12-15 19:22:03       41 阅读
  4. ELK的

    2023-12-15 19:22:03       42 阅读
  5. docker查看

    2023-12-15 19:22:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 19:22:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 19:22:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 19:22:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 19:22:03       20 阅读

热门阅读

  1. 使用 docker-compose 部署 folkmq 消息中间件

    2023-12-15 19:22:03       38 阅读
  2. Redis+threading实现多线程消息队列

    2023-12-15 19:22:03       37 阅读
  3. 数组练习(2)二分查找

    2023-12-15 19:22:03       30 阅读
  4. Python爬虫利器:BeautifulSoup库详解

    2023-12-15 19:22:03       39 阅读
  5. CS106L stream练习

    2023-12-15 19:22:03       41 阅读
  6. C# 避免定时器重入的4种方法

    2023-12-15 19:22:03       36 阅读
  7. 洛谷 P5483 小A的烦恼 题解

    2023-12-15 19:22:03       46 阅读
  8. 如何使用Composer安装和管理依赖?

    2023-12-15 19:22:03       47 阅读