@Aspect
@Component
@Slf4j
public class LoggingAspect {
@Pointcut("execution(* com.beauty.*.service.impl..*ServiceImpl.*(..))")
private void pointCutMethodService(){ }
@Pointcut("execution(* com.beauty.*.controller..*Controller.*(..))")
private void pointCutMethodController(){ }
@Around("pointCutMethodService() || pointCutMethodController()")
public Object doAroundService(ProceedingJoinPoint pjp) throws Throwable{
long begin = System.currentTimeMillis();
Object obj = pjp.proceed();
long end = System.currentTimeMillis();
//耗时查 毫秒
long timeConsuming = end-begin;
//耗时查秒
long secondConsuming = timeConsuming/1000;
//判断
String className = pjp.getSignature().getDeclaringType().getSimpleName();
// log.info("{}调用方法:{},参数:{},执行耗时:{}毫秒",className,
// pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()),timeConsuming);
if (log.isDebugEnabled()){
log.debug("{}调用方法:{},参数:{},执行耗时:{}毫秒",className,
pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()),timeConsuming);
}
if (secondConsuming >= 3){
log.error("{}调用方法:{},参数:{},执行耗时:{}毫秒,耗时已超过3秒,已超出最大相应时长。",className,
pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()),timeConsuming);
}
return obj;
}
}
1.@Aspect 注册类为aop 切面类;
2.@Pointcut 执行 execution 扫描类路径;
3.@Around 需要切面的方法;
备注:时间超时自己配置,里面打印了方法,参数等等。这里采用日志方式,也可以存入redis/mysql,由运营进行查看,进行代码优化。