Spring AOP 和 AspectJ的区别

本文探讨了SpringAOP和AspectJ之间的区别,包括它们的目标、织入方式、连接点支持、性能等方面的不同。SpringAOP侧重于方法级别的切面编程,而AspectJ提供了更为全面的AOP解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring AOP 和 AspectJ的区别

  • springAOP 是spring支持的面向切面AOP 编程。
  • AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。

1. 目标不同

springAOP 不是一个完备的AOP 方案。

AspectJ是最首创的AOP技术,用来提供全面的AOP方案。

2. 织入方式

  • AspectJ 使用了三种不同类型的织入方式:

Compile-time weaving:编译期织入。编译器将切面和应用的源代码编译在一个字节码文件中。

Post-compile weaving:编译后织入。也称为二进制织入。将已有的字节码文件与切面编制在一起。

Load-time weaving:加载时织入。与编译后织入一样,只是织入时间会推迟到类加载到jvm时。

  • springAOP使用运行时织入(runtime weaving)

在运行时织入,是使用目标对象的代理对象织入的。

springAOP的代理模式:

image.png

小结:SpringAOP 是基于动态代理的实现AOP,这意味着实现目标对象的切面会创建一个代理类(如上图,两种代理模式)。而AspectJ在程序运行期是不会做任何事情的,因为类和切面是直接编译在一起的,这种方式称为静态代理。

3. 连接点 Joinpoints

image.png

springAOP 只支持方法执行连接点,而ASpectJ 还支持 方法调用,构造方法调用,属性引用,静态初始化、其他切面的通知等 作为连接点。 功能相当强大。

4.性能

compile-time weaving is much faster than runtime weaving.

编译期织入要比运行期织入快很多。因此aspectJ 的运行速度要快于springAOP、

5.总结

This quick table summarizes the key differences between Spring AOP and AspectJ:

Spring AOPAspectJ
Implemented in pure JavaImplemented using extensions of Java programming language
No need for separate compilation processNeeds AspectJ compiler (ajc) unless LTW is set up
Only runtime weaving is availableRuntime weaving is not available. Supports compile-time, post-compile, and load-time Weaving
Less Powerful – only supports method level weavingMore Powerful – can weave fields, methods, constructors, static initializers, final class/methods, etc…
Can only be implemented on beans managed by Spring containerCan be implemented on all domain objects
Supports only method execution pointcutsSupport all pointcuts
Proxies are created of targeted objects, and aspects are applied on these proxiesAspects are weaved directly into code before application is executed (before runtime)
Much slower than AspectJBetter Performance
Easy to learn and applyComparatively more complicated than Spring AOP

github: https://crossyourheart.github.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值