0

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?

  • Then the explanation is simple: Proxy-based Spring AOP does not work with self-invocation because self-invocation does not use the aspect proxy but the original object wrapped by it, i.e. no aspect advice can be triggered. See also the [Spring manual](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-understanding-aop-proxies) (search for "self-invocation" in that chapter). The alternative would be to use full-fledged AspectJ if you need aspects to work with self-invoked code. – kriegaex May 14 '19 at 02:21

1 Answers1

0

A way can do it ,define another Service ,and move the method methodB() to it .