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

java 复制代码
@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同理

java 复制代码
@Pointcut("within(@org.springframework.stereotype.Service *) || within(@org.springframework.stereotype.Controller *)")

还可以排除

java 复制代码
@Pointcut("within(@org.springframework.stereotype.Service *) && !execution(* springfox.documentation.schema.property.*.*(..))")
相关推荐
Chen-Edward14 分钟前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
云创智城-yuncitys33 分钟前
SpringCloud 架构在智慧交通路侧停车系统中的实践:从技术落地到城市级服务升级
spring·spring cloud·架构·智慧城市·停车系统·充电系统源码
陈小桔1 小时前
idea中重新加载所有maven项目失败,但maven compile成功
java·maven
小学鸡!1 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
xiaogg36781 小时前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
逆光的July2 小时前
Hikari连接池
java
微风粼粼2 小时前
eclipse 导入javaweb项目,以及配置教程(傻瓜式教学)
java·ide·eclipse
番茄Salad2 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud
天若有情6732 小时前
Spring MVC文件上传与下载全面详解:从原理到实战
java·spring·mvc·springmvc·javaee·multipart
祈祷苍天赐我java之术2 小时前
Redis 数据类型与使用场景
java·开发语言·前端·redis·分布式·spring·bootstrap