|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
代理机制
Spring AOP 使用 JDK 动态代理或 CGLIB 为给定的目标对象创建代理。JDK 动态代理是 JDK 内置的,而 CGLIB 是一个常见的开源类定义库(重新打包为 spring-core)。
如果要代理的目标对象实现了至少一个接口,则使用JDK动态代理。目标类型实现的所有接口都将被代理。如果目标对象没有实现任何接口,则创建CGLIB代理。
如果你想强制使用CGLIB代理(例如,代理目标对象定义的每个方法,而不仅仅是它通过接口实现的方法),你可以这样做。但是,你应该考虑以下问题:
-
使用CGLIB,
final个方法无法被代理,因为它们无法在运行时生成的子类中被覆盖。 -
从 Spring 4.0 开始,你的代理对象的构造函数不会再被调用两次, 因为 CGLIB 代理实例是通过 Objenesis 创建的。只有在你的 JVM 不允许绕过构造函数的情况下, 你可能会看到两次调用以及来自 Spring 的 AOP 支持对应的调试日志条目。
要强制使用CGLIB代理,请将<aop:config>元素的proxy-target-class属性值设置为true,如下所示:
<aop:config proxy-target-class="true">
<!-- other beans defined here... -->
</aop:config>
要强制使用CGLIB代理当你使用@AspectJ自动代理支持时,请将<aop:aspectj-autoproxy>元素的proxy-target-class属性设置为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 引用进行调用,而不是代理。这有重要的含义。这意味着自我调用不会导致与方法调用相关的通知有机会运行。
好的,那么对此该怎么办?最好的方法(这里“最好”一词用得比较宽松)是重构你的代码,使得自调用不会发生。这确实需要你做一些工作,但这是最不侵入性的方法。接下来的方法绝对糟糕透顶,我们犹豫是否要指出这一点,正是因为它是如此糟糕。你可以(尽管对我们来说很痛苦)完全将类中的逻辑与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 框架。