对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
代理机制
Spring AOP 使用 JDK 动态代理或 CGLIB 为给定的代理创建代理
目标对象。JDK 动态代理内置于 JDK 中,而 CGLIB 是常见的
开源类定义库(重新打包到spring-core
).
如果要代理的目标对象至少实现了一个接口,则 JDK 动态 代理。目标类型实现的所有接口都是代理的。 如果目标对象未实现任何接口,则创建 CGLIB 代理。
如果要强制使用 CGLIB 代理(例如,代理每个方法 为目标对象定义,而不仅仅是由其接口实现的对象), 你可以这样做。但是,您应该考虑以下问题:
-
使用 CGLIB,
final
不能建议方法,因为它们不能在 运行时生成的子类。 -
从 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 Framework 提供的基于 Spring AOP 的方面。
首先考虑这样一个场景:你有一个普通的、未代理的、 nothing-special-about-it,直接对象引用,如下所示 代码片段显示:
-
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 框架。