【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.*.*(..))")
相关推荐
老板一杯拿铁19 分钟前
Java 八股文面试详解:从基础到进阶的全面梳理
java·面试
const_qiu20 分钟前
第 2.5 章 2.5.4-2.5.5:JWT 双 Token 与声明式权限守卫
java·人工智能
bug菌22 分钟前
一个周末就想把短链服务推到 10 万 QPS,真能靠加 Redis 做到吗?
java·redis·go
撸串-研究生22 分钟前
springboot福利院管理系统73979
java·spring boot·后端·福利院管理系统
不得了zz1 小时前
04-Spring AI Alibaba ReactAgent 生产级实践:工具调用、结构化输出、HITL 与上下文管理
java·ai编程·springaialibaba
猿究院_夏燊2 小时前
SpringMVC 到底是怎么干活的?
java·网络·职场和发展
鶴哥只手遮天2 小时前
Spring Framework 7.0.9 深度剖析-05-配置元数据解析:XML、注解与 JavaConfig
java·后端·开源
NGINX开源社区2 小时前
与我们交流:NGINX Gateway Fabric 与 NGINX Ingress Controller 社区会议
java·服务器·前端
鶴哥只手遮天2 小时前
Spring Framework 7.0.9 深度剖析-16-异步与调度
java·后端·开源
十年Java程序媛2 小时前
Java 多线程基础与锁机制|进程线程、synchronized、volatile、Lock 面试全梳理
java·spring boot·面试