@Component //容器管理
@Slf4j
@Aspect//当前类为切面类
public class AOPdemo {
@Around("execution(* com.example.springtlias.Service.*.*(..))")
public Object recordTime (ProceedingJoinPoint JoinPoint) throws Throwable {
long begin = System.currentTimeMillis();
Object proceed = JoinPoint.proceed();
long end = System.currentTimeMillis();
long Time = end - begin;
log.info("耗时为{}ms",Time);
return Time;
}
}
报错代码
报错信息
原因
AOP本质为动态代理,即无侵入式的增加原方法的功能,此处使用AOP后,改变了原方法的返回值所以报错。
解决
返回一个object类就ok了
@Component //容器管理
@Slf4j
@Aspect//当前类为切面类
public class AOPdemo {
@Around("execution(* com.example.springtlias.Service.*.*(..))")
public Object recordTime (ProceedingJoinPoint JoinPoint) throws Throwable {
long begin = System.currentTimeMillis();
Object proceed = JoinPoint.proceed();
long end = System.currentTimeMillis();
long Time = end - begin;
log.info("耗时为{}ms",Time);
return proceed;
}
}