代理机制

Spring AOP 使用 JDK 动态代理或 CGLIB 为给定的目标对象创建代理。JDK 动态代理内置于 JDK 中,而 CGLIB 是一个常见的开源类定义库(已重新打包到 `spring-core` 中)。

如果要代理的目标对象实现至少一个接口,则使用 JDK 动态代理,并且代理目标类型实现的所有接口。如果目标对象不实现任何接口,则创建 CGLIB 代理,它是目标类型的运行时生成的子类。

如果你想强制使用 CGLIB 代理(例如,代理目标对象上定义的每个方法,而不仅仅是其接口实现的方法),你可以这样做。但是,你应该考虑以下问题:

  • `final` 类不能被代理,因为它们不能被继承。

  • `final` 方法不能被增强,因为它们不能被覆盖。

  • `private` 方法不能被增强,因为它们不能被覆盖。

  • 不可见的方法——例如,父类中来自不同包的包私有方法——不能被增强,因为它们实际上是私有的。

  • 由于 CGLIB 代理实例是通过 Objenesis 创建的,因此你的代理对象的构造函数不会被调用两次。但是,如果你的 JVM 不允许绕过构造函数,你可能会看到双重调用以及 Spring AOP 支持对应的调试日志条目。

  • 你的 CGLIB 代理使用可能会面临 Java 模块系统的限制。典型情况下,在模块路径上部署时,你不能为 `java.lang` 包中的类创建 CGLIB 代理。这种情况需要一个 JVM 引导标志 `--add-opens=java.base/java.lang=ALL-UNNAMED`,而这个标志对于模块是不可用的。

要强制使用 CGLIB 代理,将 `<aop:config>` 元素的 `proxy-target-class` 属性设置为 true,如下所示:

<aop:config proxy-target-class="true">
	<!-- other beans defined here... -->
</aop:config>

当你使用 @AspectJ 自动代理支持时,要强制使用 CGLIB 代理,将 `<aop:aspectj-autoproxy>` 元素的 `proxy-target-class` 属性设置为 `true`,如下所示:

<aop:aspectj-autoproxy proxy-target-class="true"/>

多个 `<aop:config/>` 片段在运行时会合并成一个统一的自动代理创建器,该创建器会应用任何一个 `<aop:config/>` 片段(通常来自不同的 XML bean 定义文件)指定的最强代理设置。这也适用于 `<tx:annotation-driven/>` 和 `<aop:aspectj-autoproxy/>` 元素。

需要明确的是,在 `<tx:annotation-driven/>`、`<aop:aspectj-autoproxy/>` 或 `<aop:config/>` 元素上使用 `proxy-target-class="true"` 会强制所有这三个元素都使用 CGLIB 代理。

理解 AOP 代理

Spring AOP 是基于代理的。在你编写自己的切面或使用 Spring Framework 提供的任何基于 Spring AOP 的切面之前,理解最后这句话的实际含义至关重要。

首先考虑一个普通、未被代理的对象引用的场景,如下面的代码片段所示:

  • Java

  • Kotlin

public class SimplePojo implements Pojo {

	public void foo() {
		// this next method invocation is a direct call on the 'this' reference
		this.bar();
	}

	public void bar() {
		// some logic...
	}
}
class SimplePojo : Pojo {

	fun foo() {
		// this next method invocation is a direct call on the 'this' reference
		this.bar()
	}

	fun bar() {
		// some logic...
	}
}

如果你调用对象引用上的方法,该方法将直接在该对象引用上被调用,如下图和列表所示:

aop proxy plain pojo call
  • Java

  • Kotlin

public class Main {

	public static void main(String[] args) {
		Pojo pojo = new SimplePojo();
		// this is a direct method call on the 'pojo' reference
		pojo.foo();
	}
}
fun main() {
	val pojo = SimplePojo()
	// this is a direct method call on the 'pojo' reference
	pojo.foo()
}

当客户端代码持有的引用是代理时,情况会略有不同。考虑下面的图和代码片段:

aop proxy call
  • Java

  • Kotlin

public class Main {

	public static void main(String[] args) {
		ProxyFactory factory = new ProxyFactory(new SimplePojo());
		factory.addInterface(Pojo.class);
		factory.addAdvice(new RetryAdvice());

		Pojo pojo = (Pojo) factory.getProxy();
		// this is a method call on the proxy!
		pojo.foo();
	}
}
fun main() {
	val factory = ProxyFactory(SimplePojo())
	factory.addInterface(Pojo::class.java)
	factory.addAdvice(RetryAdvice())

	val pojo = factory.proxy as Pojo
	// this is a method call on the proxy!
	pojo.foo()
}

这里需要理解的关键是,`Main` 类中 `main(..)` 方法内部的客户端代码持有一个对代理的引用。这意味着在该对象引用上的方法调用是针对代理的调用。因此,代理可以将调用委托给与该特定方法调用相关的所有拦截器(通知)。然而,一旦调用最终到达目标对象(在本例中是 `SimplePojo` 引用),它可能对自身进行的任何方法调用,例如 `this.bar()` 或 `this.foo()`,都将针对 `this` 引用而不是代理进行调用。这具有重要的影响。这意味着自调用不会导致与方法调用关联的通知有机会运行。换句话说,通过显式或隐式的 `this` 引用进行的自调用将绕过通知。

为了解决这个问题,你有以下选项。

避免自调用

最好的方法(这里的“最好”只是一个笼统的说法)是重构你的代码,以便不会发生自调用。这确实需要你做一些工作,但它是最好的、侵入性最小的方法。

注入自引用

另一种方法是利用自注入,并通过自引用而不是通过 this 调用代理上的方法。

使用 AopContext.currentProxy()

最后这种方法强烈不推荐使用,我们倾向于前两种选项,不愿指出此方法。然而,作为最后的手段,你可以选择将类中的逻辑与 Spring AOP 绑定,如下面的示例所示。

  • Java

  • Kotlin

public class SimplePojo implements Pojo {

	public void foo() {
		// This works, but it should be avoided if possible.
		((Pojo) AopContext.currentProxy()).bar();
	}

	public void bar() {
		// some logic...
	}
}
class SimplePojo : Pojo {

	fun foo() {
		// This works, but it should be avoided if possible.
		(AopContext.currentProxy() as Pojo).bar()
	}

	fun bar() {
		// some logic...
	}
}

使用 AopContext.currentProxy() 会将你的代码完全耦合到 Spring AOP,并且使类本身知道它正在 AOP 上下文中使用,这会降低 AOP 的部分优势。它还要求配置 ProxyFactory 以暴露代理,如下面的示例所示:

  • Java

  • Kotlin

public class Main {

	public static void main(String[] args) {
		ProxyFactory factory = new ProxyFactory(new SimplePojo());
		factory.addInterface(Pojo.class);
		factory.addAdvice(new RetryAdvice());
		factory.setExposeProxy(true);

		Pojo pojo = (Pojo) factory.getProxy();
		// this is a method call on the proxy!
		pojo.foo();
	}
}
fun main() {
	val factory = ProxyFactory(SimplePojo())
	factory.addInterface(Pojo::class.java)
	factory.addAdvice(RetryAdvice())
	factory.isExposeProxy = true

	val pojo = factory.proxy as Pojo
	// this is a method call on the proxy!
	pojo.foo()
}
AspectJ 编译时织入和加载时织入没有这种自调用问题,因为它们是在字节码内部应用通知,而不是通过代理。