对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

代理机制

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

如果待代理的目标对象实现了至少一个接口,将使用JDK动态代理,并且目标类型实现的所有接口都将被代理。 如果目标对象没有实现任何接口,将创建一个CGLIB代理,它是目标类型的运行时生成的子类。spring-doc.cadn.net.cn

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

  • final 个类无法被代理,因为它们无法被继承。spring-doc.cadn.net.cn

  • final 个方法无法被通知,因为它们不能被重写。spring-doc.cadn.net.cn

  • private 个方法无法被通知,因为它们不能被重写。spring-doc.cadn.net.cn

  • 在不同包中的父类里,不可见的方法——例如,包私有的方法——无法被增强,因为它们实际上等同于私有方法。spring-doc.cadn.net.cn

  • 您的代理对象的构造函数不会被调用两次,因为CGLIB代理实例是通过Objenesis创建的。但是,如果您的JVM不允许绕过构造函数,您可能会看到双重调用以及来自Spring AOP支持的相关调试日志条目。spring-doc.cadn.net.cn

  • 您的CGLIB代理使用可能受到Java模块系统限制。作为一个典型情况,当在模块路径上部署时,您无法为来自java.lang包的类创建CGLIB代理。这类情况需要JVM引导标志--add-opens=java.base/java.lang=ALL-UNNAMED,而模块不提供此功能。spring-doc.cadn.net.cn

要强制使用CGLIB代理,请将<aop:config>元素的proxy-target-class属性值设置为true,如下所示:spring-doc.cadn.net.cn

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

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

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

多个 <aop:config/> 部分在运行时被合并为一个统一的自动代理创建器,它应用了任何 <aop:config/> 部分(通常来自不同的XML bean定义文件)指定的最强代理设置。这也适用于 <tx:annotation-driven/><aop:aspectj-autoproxy/> 元素。spring-doc.cadn.net.cn

要明确的是,在proxy-target-class="true"<tx:annotation-driven/><aop:aspectj-autoproxy/><aop:config/>元素上使用proxy-target-class="true"会强制使用CGLIB代理对所有三个元素spring-doc.cadn.net.cn

理解AOP代理

Spring AOP 是基于代理的。在编写自己的切面或使用 Spring 框架提供的任何基于 Spring AOP 的切面之前,至关重要的是要理解这一陈述的实际含义。spring-doc.cadn.net.cn

首先考虑一个场景,你拥有一个普通、未经代理的对象引用,如下代码片段所示:spring-doc.cadn.net.cn

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...
	}
}

如果你在一个对象引用上调用一个方法,该方法将直接在该对象引用上调用,如下图和代码示例所示:spring-doc.cadn.net.cn

aop proxy plain pojo call
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()
}

当客户端代码持有的引用是一个代理时,情况会略有不同。请考虑以下图表和代码片段:spring-doc.cadn.net.cn

aop proxy call
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引用进行的自我调用将会绕过建议。spring-doc.cadn.net.cn

为了解决这个问题,您有以下选项。spring-doc.cadn.net.cn

避免自我调用

最佳的做法(这里“最佳”一词使用得较为宽松)是重构你的代码,以避免自我调用的发生。这确实需要你做一些工作,但这是最佳的、侵入性最小的方法。spring-doc.cadn.net.cn

注入自我引用

另一种方法是利用 自我注入, 并通过代理的自我引用调用方法,而不是通过thisspring-doc.cadn.net.cn

使用 AopContext.currentProxy()

最后这种方法是极不推荐的,我们不愿提及它,相比之下,前面提到的选项更为可取。然而,作为最后的手段,你可以选择将类内的逻辑与Spring AOP绑定,如下例所示。spring-doc.cadn.net.cn

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以暴露代理,如下例所示:spring-doc.cadn.net.cn

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 的编译时织入和加载时织入不存在自我调用问题,因为它们是在字节码内部应用通知,而非通过代理方式。