package com.ainemo.edu.dating.service.impl; import lombok.extern.slf4j.Slf4j; 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 java.util.Arrays; import java.util.List; /** * 日志AOP切面 * * @author jiangwentao * @date 2019/12/24 */ @Aspect @Slf4j @Component public class LogAspect { @Pointcut("execution(* com.ainemo.edu.dating.service.*.*(..))") public void serviceLogPointCut() { } /** * 方法进入前置通知 打印方法名 请求参数 * * @param joinPoint 切入点 */ @Before(value = "serviceLogPointCut()") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); String className = joinPoint.getTarget().getClass().getSimpleName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); log.debug("-----------start-----------"); log.debug("method:[{}.{}] begin with param:[{}]", className, methodName, args); } /** * 方法返回后通知 打印方法名 结果 * * @param joinPoint 切入点 * @param result 结果 */ @AfterReturning(value = "serviceLogPointCut()", returning = "result") public void returnMethod(JoinPoint joinPoint, Object result) { String methodName = joinPoint.getSignature().getName(); String className = joinPoint.getTarget().getClass().getSimpleName(); log.debug("method:[{}.{}] end with result:[{}]", className, methodName, result); log.debug("-----------end-----------"); } }
AOP同一日志打印实现案例
最新推荐文章于 2020-09-14 15:21:53 发布