内部和私有方法Java的AOP

时间:2017-03-16 11:33:45

标签: java spring aspectj spring-aop

我正在尝试记录使用自定义界面注释的方法的执行时间。

我正在使用Spring AOP。

但这似乎不适用于内部方法。

我认为这是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;
  }

}

除Spring AOP之外还有其他选择

1 个答案:

答案 0 :(得分:2)

如果您考虑一下Spring处理AOP注释的方式,这将是明确的:

Spring将您的类包装在一个代理中,并通过添加的AOP注释动态生成额外的代码。因此,只有通过代理调用的代码(即来自课外的代码)才会被包含在内。

实施例

@Service
public class Foo {

  public void doSomething() {
      doSomethinInternal();
  }

  private void doSomethingInternal() {
  }
}

如果从另一个Spring bean我这样做:

@Service
public class Bar {

  @Autowired
  private Foo foo;

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

只有doSomething将通过包裹你的类的代理调用,而不是doSomethingInternal,这将由你的类调用。