|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
代理机制
Spring AOP 使用 JDK 动态代理或 CGLIB 来为目标对象创建代理。JDK 动态代理内置于 JDK 中,而 CGLIB 是一个常用的开源类定义库(已重新打包到 spring-core 中)。
如果要被代理的目标对象实现了至少一个接口,则使用 JDK 动态代理。目标类型所实现的所有接口都会被代理。 如果目标对象未实现任何接口,则会创建一个 CGLIB 代理。
如果你想强制使用 CGLIB 代理(例如,代理目标对象定义的所有方法,而不仅仅是其接口中实现的方法),你可以这样做。但是,你应该考虑以下问题:
-
使用 CGLIB 时,
final方法无法被通知(advised),因为它们不能在运行时生成的子类中被重写。 -
从 Spring 4.0 开始,您的代理对象的构造函数不会再被调用两次, 因为 CGLIB 代理实例是通过 Objenesis 创建的。只有在您的 JVM 不允许绕过构造函数的情况下,您才可能会看到构造函数被调用两次, 并看到来自 Spring AOP 支持的相应调试日志条目。
要强制使用 CGLIB 代理,请将 proxy-target-class 元素的 <aop:config> 属性值设置为 true,如下所示:
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
当你使用 @AspectJ 自动代理支持时,若要强制使用 CGLIB 代理,请将 proxy-target-class 元素的 <aop:aspectj-autoproxy> 属性设置为 true,如下所示:
<aop:aspectj-autoproxy proxy-target-class="true"/>
|
多个 需要明确的是,在 |
理解 AOP 代理
Spring AOP 基于代理。在编写自己的切面或使用 Spring 框架所提供的任何基于 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 引用本身,而不是代理对象。
这一点具有重要的影响:它意味着自调用(self-invocation)不会触发与该方法调用相关联的通知逻辑执行。
那么,对此该怎么办呢?最好的方法(此处“最好”一词用得较为宽松)是重构你的代码,以避免发生自调用。这确实需要你做一些工作,但这是最佳且侵入性最小的方法。 下一种方法则完全糟糕透顶,我们甚至犹豫是否要提出来,正是因为这种方法实在太糟糕了。你可以(尽管对我们而言这很痛苦)将类中的逻辑完全与 Spring AOP 绑定在一起,如下例所示:
-
Java
-
Kotlin
public class SimplePojo implements Pojo {
public void foo() {
// this works, but... gah!
((Pojo) AopContext.currentProxy()).bar();
}
public void bar() {
// some logic...
}
}
class SimplePojo : Pojo {
fun foo() {
// this works, but... gah!
(AopContext.currentProxy() as Pojo).bar()
}
fun bar() {
// some logic...
}
}
这完全将您的代码与 Spring AOP 耦合在一起,并且使得该类自身意识到它正被用于 AOP 上下文中,这违背了 AOP 的初衷。此外,如以下示例所示,在创建代理时还需要一些额外的配置:
-
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 没有这种自调用问题,因为它不是基于代理的 AOP 框架。