用于捕获绕过 Spring 缓存 @Cacheable 方法的自调用的静态分析工具

Static Analysis tool to catch self-invocation bypassing Spring cache @Cacheable method

据我所知,这是因为在 Spring 中创建代理以处理缓存、事务相关功能的方式。修复它的方法是使用 AspectJ,但我不想走那条路,因为它有自己的问题。 我可以使用任何静态分析工具检测自调用吗?

@Cacheable(value = "defaultCache", key = "#id")
public Person findPerson(int id) {
   return getSession().getPerson(id);
} 

public List<Person> findPersons(int[] ids) {
   List<Person> list = new ArrayList<Person>();
       for (int id : ids) {
      list.add(findPerson(id));
    }
   return list;
} 

Can I detect self-invocation using any static analysis tools?

理论上可以,但要注意Rice's theorem。任何此类工具有时都会发出错误警报。

您可以使用 abstract interpretation 技术开发这样的工具。你可能需要一年多的工作。

您可以将此类工具的开发分包给例如Frama-C 团队。然后给我发邮件到 basile.starynkevitch@cea.fr

如果检测内部调用就足够了,您可以为此使用本机 AspectJ 而不是 Spring AOP,然后在每次发生这种情况时抛出 运行 时间异常或记录警告。那不是静态分析,但聊胜于无。另一方面,如果您使用本机 AspectJ,则无论如何您不限于 Spring 代理,并且方面也可以用于自调用。

无论如何,这就是一个方面的样子,包括一个 MCVE 展示它是如何工作的。我是在 Spring 之外完成的,这就是为什么我使用代理 @Component 注释来进行演示。

更新: 抱歉,目标是 @Component classes 而不是 @Cacheable classes/methods,但是如果您只是稍微调整一下切入点,我在这里展示的基本相同的一般方法也适用于您的具体情况。

组件注释:

package de.scrum_master.app;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(TYPE)
public @interface Component {}

示例 classes(组件和非组件):

这个组件是要被其他组件调用的,应该不会导致exceptions/warnings:

package de.scrum_master.app;

@Component
public class AnotherComponent {
  public void doSomething() {
    System.out.println("Doing something in another component");
  }
}

此 class 不是 @Component,因此方面应忽略其中的自调用:

package de.scrum_master.app;

public class NotAComponent {
  public void doSomething() {
    System.out.println("Doing something in non-component");
    new AnotherComponent().doSomething();
    internallyCalled("foo");
  }

  public int internallyCalled(String text ) {
    return 11;
  }
}

这个class是一个@Component。方面应该标记 internallyCalled("foo"),而不是 new AnotherComponent().doSomething()

package de.scrum_master.app;

@Component
public class AComponent {
  public void doSomething() {
    System.out.println("Doing something in component");
    new AnotherComponent().doSomething();
    internallyCalled("foo");
  }

  public int internallyCalled(String text ) {
    return 11;
  }
}

驱动申请:

请注意,我在整个示例代码中使用 new 创建组件实例,而不是像我在 Spring 中那样从应用程序上下文请求 bean。但是你可以忽略它,这只是一个例子。

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    new NotAComponent().doSomething();
    new AComponent().doSomething();
  }
}

当 运行out 方面时的控制台日志:

Doing something in non-component
Doing something in another component
Doing something in component
Doing something in another component

现在有了方面,而不是我们期望的最后一条消息是异常或记录的警告。以下是如何做到这一点:

看点:

很抱歉在这里使用本机 AspectJ 语法。当然,你也可以使用基于注解的语法。

package de.scrum_master.aspect;

import de.scrum_master.app.*;

public aspect SelfInvocationInterceptor {
  Object around(Object caller, Object callee) :
    @within(Component) &&
    call(* (@Component *).*(..)) &&
    this(caller) &&
    target(callee)
  {
    if (caller == callee)
      throw new RuntimeException(
        "Self-invocation in component detected from "  + thisEnclosingJoinPointStaticPart.getSignature() +
        " to "+ thisJoinPointStaticPart.getSignature()
      );
    return proceed(caller, callee);
  }
}

运行 方面时的控制台日志:

Doing something in non-component
Doing something in another component
Doing something in component
Doing something in another component
Exception in thread "main" java.lang.RuntimeException: Self-invocation in component detected from void de.scrum_master.app.AComponent.doSomething() to int de.scrum_master.app.AComponent.internallyCalled(String)
    at de.scrum_master.app.AComponent.internallyCalled_aroundBody3$advice(AComponent.java:8)
    at de.scrum_master.app.AComponent.doSomething(AComponent.java:8)
    at de.scrum_master.app.Application.main(Application.java:6)

我认为,您可以使用此解决方案,也许更愿意记录警告而不是抛出异常,以便温和地指导您的同事检查和改进他们的 AOP 相关 Spring 组件。有时,也许他们无论如何都不希望自调用触发某个方面,这取决于具体情况。您可以在完整的 AspectJ 模式下 运行 Spring 应用程序,然后在评估日志后,切换回 Spring AOP。但也许从一开始就使用本机 AspectJ 并完全避免自调用问题会更简单。


更新: 在 AspectJ 中,如果满足某些条件,您还可以让编译器抛出警告或错误。在这种情况下,您只能静态地确定组件对其他组件的调用,而不会区分自调用和其他组件对其他方法的调用。所以这对你没有帮助。

另请注意,此解决方案仅限于由 @Component 注释的 classes。如果您的 Spring bean 以其他方式实例化,例如通过 XML 配置或 @Bean 工厂方法,这个简单的方面不起作用。但它可以很容易地通过检查拦截的 class 是否是代理实例来扩展,然后才决定标记自调用。不幸的是,您必须将方面代码编织到所有应用程序 classes 中,因为检查只能在 运行 时间期间发生。

我可以解释更多的事情,例如使用自注入并在注入的代理实例上调用内部方法而不是通过 this.internallyCalled(..)。那么自调用问题也将得到解决,这种方法也适用于Spring AOP。