Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AOP for inner and private methods Java

I am trying to log the execution time for methods annotated with custom interface.

I am using Spring AOP.

But this does not seems to work for inner methods.

I think it is the limitation in Spring AOP

@Aspect
public class BusinessProfiler {

  private static Log log = LogFactory.getLog(BusinessProfiler.class);


  @Around("execution(* *(..)) && @annotation(TimeLog)")
  public Object profile(ProceedingJoinPoint point) throws Throwable {
    long start = System.currentTimeMillis();
    Object result = point.proceed();
    String format =
        String.format("%s#%s: took [%s msec]", point.getTarget().getClass().getSimpleName(),
            MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
            System.currentTimeMillis() - start);
    log.info(format);
    return result;
  }

}

Are there any alternatives than Spring AOP

like image 678
Patan Avatar asked May 22 '26 04:05

Patan


1 Answers

If you think about the way AOP annotations are dealt with by Spring this will be clear:

Spring takes your class and wraps it in a proxy with the extra code generated on the fly by the AOP annotation added. So only code called via the proxy (i.e from outside your class will be included).

Example

@Service
public class Foo {

  public void doSomething() {
      doSomethingInternal();
  }

  public void doSomethingInternal() {
  }
}

If from another Spring bean I do this:

@Service
public class Bar {

  @Autowired
  private Foo foo;

  public void execute() {
      foo.doSomething();
  }
}

Only doSomething will be called via the proxy which wraps your class, not doSomethingInternal, that will be called by your class.

like image 128
Essex Boy Avatar answered May 24 '26 17:05

Essex Boy