Spring AOP 和 AspectJ的区别
Spring AOP 和 AspectJ的區(qū)別
- springAOP 是spring支持的面向切面AOP 編程。
- AspectJ是一個(gè)面向切面的框架,它擴(kuò)展了Java語(yǔ)言。AspectJ定義了AOP語(yǔ)法,它有一個(gè)專門的編譯器用來(lái)生成遵守Java字節(jié)編碼規(guī)范的Class文件。
1. 目標(biāo)不同
springAOP 不是一個(gè)完備的AOP 方案。
AspectJ是最首創(chuàng)的AOP技術(shù),用來(lái)提供全面的AOP方案。
2. 織入方式
- AspectJ 使用了三種不同類型的織入方式:
Compile-time weaving:編譯期織入。編譯器將切面和應(yīng)用的源代碼編譯在一個(gè)字節(jié)碼文件中。
Post-compile weaving:編譯后織入。也稱為二進(jìn)制織入。將已有的字節(jié)碼文件與切面編制在一起。
Load-time weaving:加載時(shí)織入。與編譯后織入一樣,只是織入時(shí)間會(huì)推遲到類加載到j(luò)vm時(shí)。
- springAOP使用運(yùn)行時(shí)織入(runtime weaving)
在運(yùn)行時(shí)織入,是使用目標(biāo)對(duì)象的代理對(duì)象織入的。
springAOP的代理模式:
小結(jié):SpringAOP 是基于動(dòng)態(tài)代理的實(shí)現(xiàn)AOP,這意味著實(shí)現(xiàn)目標(biāo)對(duì)象的切面會(huì)創(chuàng)建一個(gè)代理類(如上圖,兩種代理模式)。而AspectJ在程序運(yùn)行期是不會(huì)做任何事情的,因?yàn)轭惡颓忻媸侵苯泳幾g在一起的,這種方式稱為靜態(tài)代理。
3. 連接點(diǎn) Joinpoints
springAOP 只支持方法執(zhí)行連接點(diǎn),而ASpectJ 還支持 方法調(diào)用,構(gòu)造方法調(diào)用,屬性引用,靜態(tài)初始化、其他切面的通知等 作為連接點(diǎn)。 功能相當(dāng)強(qiáng)大。
4.性能
compile-time weaving is much faster than runtime weaving.
編譯期織入要比運(yùn)行期織入快很多。因此aspectJ 的運(yùn)行速度要快于springAOP、
5.總結(jié)
This quick table summarizes the key differences between Spring AOP and AspectJ:
| Implemented in pure Java | Implemented using extensions of Java programming language |
| No need for separate compilation process | Needs AspectJ compiler (ajc) unless LTW is set up |
| Only runtime weaving is available | Runtime weaving is not available. Supports compile-time, post-compile, and load-time Weaving |
| Less Powerful – only supports method level weaving | More Powerful – can weave fields, methods, constructors, static initializers, final class/methods, etc… |
| Can only be implemented on beans managed by Spring container | Can be implemented on all domain objects |
| Supports only method execution pointcuts | Support all pointcuts |
| Proxies are created of targeted objects, and aspects are applied on these proxies | Aspects are weaved directly into code before application is executed (before runtime) |
| Much slower than AspectJ | Better Performance |
| Easy to learn and apply | Comparatively more complicated than Spring AOP |
github: https://crossyourheart.github.io/
總結(jié)
以上是生活随笔為你收集整理的Spring AOP 和 AspectJ的区别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java中实例化对象是什么意思
- 下一篇: 9.SpringCloud Gatewa