此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

代理机制

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

如果要代理的目标对象至少实现了一个接口,则 JDK 动态 proxy,并且目标类型实现的所有接口都被代理。 如果目标对象未实现任何接口,则会创建一个 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打包时 在模块路径上部署。这种情况需要 JVM 引导标志--add-opens=java.base/java.lang=ALL-UNNAMED这不适用于模块。spring-doc.cadn.net.cn

强制特定的AOP代理类型

要强制使用 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

@EnableAspectJAutoProxy,@EnableTransactionManagement和相关配置 注释提供相应的proxyTargetClass属性。这些都塌陷了 也转换为单个统一的自动代理创建器,从而在运行时有效地应用最强的代理设置。从 7.0 开始,这适用于各个代理处理器 例如,还有@EnableAsync,始终如一地参与统一全球 给定应用程序中所有自动代理尝试的默认设置。spring-doc.cadn.net.cn

全局默认代理类型可能因设置而异。虽然核心框架 默认建议基于接口的代理,Spring Boot 可能 - 取决于 配置属性 - 默认启用基于类的代理。spring-doc.cadn.net.cn

从 7.0 开始,可以通过以下方式为单个 Bean 强制使用特定的代理类型 这@Proxyable给定的注释@Beanmethod 或@Component类,使用@Proxyable(INTERFACES)@Proxyable(TARGET_CLASS)覆盖任何全局 配置默认值。对于非常具体的目的,您甚至可以指定代理 要通过@Proxyable(interfaces=…​),限制曝光 到选定的接口,而不是目标 Bean 实现的所有接口。spring-doc.cadn.net.cn

了解 AOP 代理

Spring AOP 是基于代理的。掌握 在你编写自己的方面或使用任何方面之前,最后一个陈述的实际含义 Spring Framework 提供的基于 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引用,而不是代理。 这具有重要意义。这意味着自我调用不会产生 在与方法调用相关的建议中,获得运行的机会。换句话说, 通过显式或隐式的自我调用this参考将绕过建议。spring-doc.cadn.net.cn

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

避免自我调用

最好的方法(此处松散地使用术语“最佳”)是重构代码,例如 自我调用不会发生。这确实需要您做一些工作,但是 这是最好的、侵入性最小的方法。spring-doc.cadn.net.cn

注入自我引用

另一种方法是利用自我注射, 并通过自引用而不是通过this.spring-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编译时编织和加载时编织没有这种自调用 问题,因为它们在字节码内而不是通过代理应用建议。