I define a aspect as follow:
@Component
@Aspect
public class ServiceMethodExecuteTimeAspect {
private Logger logger = LoggerFactory.getLogger(getClass());
@Pointcut("execution(* *.service.*.*(..))")
public void serviceMethod() {
}
@Around("serviceMethod()")
public Object executeTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result;
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
long executeTime;
long start;
try {
start = System.currentTimeMillis();
result = proceedingJoinPoint.proceed();
long end = System.currentTimeMillis();
executeTime = end - start;
logger.debug("ServiceMethodExecuteTimeAspect class method:{}#{} execute time is:{}", proceedingJoinPoint.getTarget().getClass().getName(), method.getName(), executeTime);
return result;
} catch (Throwable throwable) {
logger.error("ServiceMethodExecuteTimeAspect throw exception", throwable);
throw throwable;
}
}
}
And I have a Service class in a service package as follow:
package example.service;
@Service
public class AService{
public void methodA(){methodB();}
public void methodB(){}
}
then ,when create AService
instance bean get the follow log:
DEBUG o.s.a.f.CglibAopProxy - Unable to apply any optimizations to advised method: public example.AService.methodB();
Then a spring component
invoke the AService.methodA()
method and I want see the log execution time of AService.methodA()
and also log the execution time of AService.methodB()
,but I find it's only log execution time of AService.methodA()
.
In an another word my ServiceMethodExecuteTimeAspect
not work on AService.methodB()
when I invoke AService.methodA()
.
So how can I make the aspect work on the method AService.methodB()
when I I invoke AService.methodA()
method?