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

代理机制

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

如果要代理的目标对象至少实现了一个接口,则使用 JDK 动态代理。目标类型实现的所有接口都被代理。如果目标对象未实现任何接口,则会创建一个 CGLIB 代理。spring-doc.cadn.net.cn

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

  • 使用 CGLIB,final不能建议方法,因为它们不能在runtime-generated 子类中被覆盖。spring-doc.cadn.net.cn

  • 从 Spring 4.0 开始,代理对象的构造函数不再被调用两次,因为 CGLIB 代理实例是通过 Objenesis 创建的。只有当你的 JVM 这样做不允许绕过构造函数时,你可能会看到双重调用和来自 Spring 的 AOP 支持的相应调试日志条目。spring-doc.cadn.net.cn

  • 您的 CGLIB 代理使用可能会面临 JDK 9+ 平台模块系统的限制。作为典型情况,您无法从java.langpackage 时。这种情况需要 JVM 引导标志--add-opens=java.base/java.lang=ALL-UNNAMED这不适用于模块。spring-doc.cadn.net.cn

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

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

要在使用 @AspectJ 自动代理支持时强制 CGLIB 代理,请将proxy-target-class属性的<aop:aspectj-autoproxy>元素设置为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/>元素强制使用 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 代理 Plain Pojo 调用
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 代理调用
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-doc.cadn.net.cn

好吧,那么该怎么做呢?最好的方法(这里使用术语“最佳”是松散地)是重构你的代码,这样就不会发生自我调用。这确实需要你做一些工作,但这是最好的、侵入性最小的方法。下一种方法绝对是可怕的,我们犹豫是否要指出它,确切地说因为它太可怕了。你可以(尽管这对我们来说很痛苦)将逻辑与 Spring AOP 完全联系起来,如以下示例所示:spring-doc.cadn.net.cn

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 背道而驰。 它 在创建代理时还需要一些额外的配置,如以下示例所示: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 没有这种自我调用问题,因为它不是一个基于代理的 AOP 框架。spring-doc.cadn.net.cn