zoukankan      html  css  js  c++  java
  • 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 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
  • 相关阅读:
    spring 循环依赖问题
    spring data jpa 关键字 命名
    mongodb 添加字段并设置默认值
    java mongoTemplate的group统计
    java8 从对象集合中取出某个字段的集合
    springboot12-zuul
    springboot11-01-security入门
    springboot项目怎么部署到外部tomcat
    springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
    UML
  • 原文地址:https://www.cnblogs.com/paidaxing7090/p/13893612.html
Copyright © 2011-2022 走看看