Spring AOP 和 AspectJ的区别
-
springAOP 是spring支持的面向切面AOP 编程。
-
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法,它有一个专门的
1. 目标不同
springAOP 不是一个完备的AOP 方案。
AspectJ是最首创的AOP技术,用来提供全面的AOP方案。
2. 织入方式
-
AspectJ 使用了三种不同类型的织入方式:
Compile-time weaving:编译期织入。编译器将切面和应用的源代码编译在一个字节码文件中。
Post-compile weaving:编译后织入。也称为二进制织入。将已有的字节码文件与切面编制在一起。
Load-time weaving:加载时织入。与编译后织入一样,只是织入时间会推迟到类加载到jvm时。
-
springAOP使用运行时织入(runtime weaving)
在运行时织入,是使用目标对象的代理对象织入的。
springAOP的代理模式:
小结:SpringAOP 是基于动态代理的实现AOP,这意味着实现目标对象的切面会创建一个代理类(如上图,两种代理模式)。而AspectJ在程序运行期是不会做任何事情的,因为类和切面是直接编译在一起的,这种方式称为静态代理。
3. 连接点 Joinpoints
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 AOP | 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 |