代理机制
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 代理
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...
}
}
如果您在对象引用上调用方法,该方法将直接在该对象引用上调用,如下图和列表所示:

-
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()
}
当客户端代码拥有的引用是一个代理时,情况会略有不同。请看下图和代码片段:

-
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 编译时织入和加载时织入没有这个自调用问题,因为它们在字节码中应用通知,而不是通过代理。 |