Spring AOP 和 AspectJ的区别

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/

  • 4
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOPAspectJ AOP有以下几个区别: 1. 目标不同:Spring AOPSpring框架支持的面向切面编程的一部分,而AspectJ是一个独立的面向切面的框架,它扩展了Java语言并定义了AOP语法。 2. 实现方式不同:Spring AOP是在运行时进行织入的,只能针对方法进行AOP,无法针对构造函数、字段进行AOPAspectJ可以在编译成class时就进行织入,还提供了后编译器织入和类加载期织入的方式。 3. 功能差异:由于AspectJ是一个更强大的框架,它提供了更丰富的AOP功能。除了支持方法级别的切面,AspectJ还可以对构造函数、字段进行切面处理,还支持更复杂的切面表达式和切入点定义。 4. 选用原则:选择使用Spring AOP还是AspectJ AOP取决于具体的需求和项目情况。如果只需要简单的切面功能,并且已经使用了Spring框架,那么Spring AOP可以满足大部分需求。但如果需要更高级的AOP功能,或者需要在编译期进行织入,那么可以考虑使用AspectJ。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring AOPAspectJ 之间的差别](https://blog.csdn.net/xiashenbao/article/details/119953180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Spring AOPAspectJ区别](https://blog.csdn.net/qq_36259539/article/details/109140778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值