java - 用于内部方法和私有(private)方法的 AOP Java

标签 java spring aspectj spring-aop

我正在尝试记录使用自定义接口(interface)注释的方法的执行时间。

我正在使用 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还有其他选择吗

最佳答案

如果您考虑一下 Spring 处理 AOP 注释的方式,就会很清楚:

Spring 获取您的类并将其包装在代理中,并添加由 AOP 注释动态生成的额外代码。因此,只有通过代理调用的代码(即从您的类(class)外部调用的代码)才会被包括在内。

例子

@Service
public class Foo {

  public void doSomething() {
      doSomethingInternal();
  }

  public void doSomethingInternal() {
  }
}

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

@Service
public class Bar {

  @Autowired
  private Foo foo;

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

只有 doSomething 会通过包装您的类的代理调用,而不会被您的类调用的 doSomethingInternal。

关于java - 用于内部方法和私有(private)方法的 AOP Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832946/

相关文章:

spring - Grails 不使用 message.properties 来显示错误消息?

Spring 3.0/AOP/Aspectj :autoproxy intercept any call to getConnection()

java new File() 卡在陈旧的 NFS 挂载点上

java - 谷歌分析 : Get access token from refresh token

java - 在 Spring Boot 中实现 'logout' 功能

java - 如何使用 Spring Security 识别 guest 用户

java - Spring中的Kotlin setter依赖注入(inject)

java - Adviceexecution() 在 AspectJ 中不起作用

java - Spring @EnableRetry 抛出 InternalAutoProxyCreator

java - hibernate-commons-annotations-4.0.1.Final.jar;无效的 LOC header (错误的签名)?