SpringAop与AspectJ的联系与区别

区别

AspectJ

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

spring aop

Spring提供了四种类型的Aop支持
* 基于经典的SpringAOP
* 纯POJO切面
* @ASpectJ注解驱动的切面
* 注入式AspectJ切面(其实与Spring并无多大的关系,这个就是使用AspectJ这个框架实现Aop编程)

基于经典的SpringAop

其使用ProxyFactoryBean创建:
增强(通知)的类型有:
前置通知:org.springframework.aop.MethodBeforeAdvice
后置通知:org.springframework.aop.AfterReturningAdvice
环绕通知:org.aopalliance.intercept.MethodInterceptor
异常通知:org.springframework.aop.ThrowsAdvice

public interface IBookDao {public int add()public int delete();
}public class BookDaoImpl implements IBookDao{public int add() {System.out.println("正在添加图书...");return 0;}public int delete() {System.out.println("正在删除图书...");return 0;}
}//实现了MethodInterceptor的环绕增强类
public class MyAdvice implements MethodInterceptor{public Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("Around Advice before method invocation");Object o = invocation.proceed();System.out.println("Around Advice after method invocation");return o;}
}
//将每一个连接点都当做切点(拦截每一个方法)
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean><bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean><bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="bookDao"/><property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/><property name="interceptorNames" value="myadvice"/></bean>12345678910
使用RegexMethodPointcutAdvisor针对某些特定的方法进行拦截增强
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean><bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean><bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"--><property name="patterns" value=".*add"/><property name="advice" ref="myadvice"/></bean>
<!--使用的时候使用这个id,而不是原始的那个id--><bean id="bookDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="bookDao"/><property name="proxyInterfaces" value="com.njust.learning.spring.service.IBookDao"/><property name="interceptorNames" value="rmpAdvisor"/></bean>
注意

像上面这样,每定义一个dao都需要定义一个ProxyFactoryBean,显得很麻烦,所以我们引入自动代理,也就是自动创建代理对象

BeanNameAutoProxyCreator

<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean><bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean><bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"--><property name="patterns" value=".*add"/><property name="advice" ref="myadvice"/></bean><bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames" value="*Dao"></property><property name="interceptorNames" value="rmpAdvisor"></property></bean>

DefaultAdvisorAutoProxyCreator

<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean><bean id="myadvice" class="com.njust.learning.spring.aop.MyAdvice"></bean><bean id="rmpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><!--patterns,如果有多个指定的值的话,可以使用,隔开,例如value=".*add,.*delete"--><property name="patterns" value=".*add"/><property name="advice" ref="myadvice"/></bean><!--根据切面中生成信息生成代理--><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>1234567891011

纯POJO切面,需要使用XML进行配置

public interface IBookDao {public int add();public int delete();
}public class BookDaoImpl implements IBookDao{public int add() {int a = 1/0;System.out.println("正在添加图书...");return 0;}public int delete() {System.out.println("正在删除图书...");return 0;}
}
public class PojoAdvice {public void before(){System.out.println("前置通知");}public void after(Object returnval){System.out.println("后置通知"+",处理后的结果为:"+returnval);}public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("环绕前置增强...");Object o = proceedingJoinPoint.proceed();System.out.println("环绕后置增强...");return o;}public void afterThrowing(Throwable e){System.out.println("异常通知:"+e.getMessage());}
}
<bean id="bookDao" class="com.njust.learning.spring.service.BookDaoImpl"></bean><bean id="pojoAdvice" class="com.njust.learning.spring.pojoaop.PojoAdvice"></bean><aop:config><aop:pointcut id="p" expression="execution (* *.add(..))"/><aop:aspect ref="pojoAdvice"><aop:before method="before" pointcut-ref="p"></aop:before><!--通过设置returning来将返回值传递给通知--><aop:after-returning method="after" pointcut-ref="p" returning="returnval"/><aop:around method="around" pointcut-ref="p"/><!--通过设置returning来将异常对象传递给通知--><aop:after-throwing method="afterThrowing" pointcut-ref="p" throwing="e"/></aop:aspect></aop:config>

联系

我们借助于Spring Aop的命名空间可以将纯POJO转换为切面,实际上这些POJO只是提供了满足切点的条件时所需要调用的方法,但是,这种技术需要XML进行配置,不能支持注解
所以spring借鉴了AspectJ的切面,以提供注解驱动的AOP,本质上它依然是Spring基于代理的AOP,只是编程模型与AspectJ完全一致,这种风格的好处就是不需要使用XML进行配置




比较分析 Spring AOP 和 AspectJ 之间的差别

AOP(Aspect OrientedProgramming, 面向切面/方面编程) 旨在从业务逻辑中分离出来横切逻辑【eg:性能监控、日志记录、权限控制等】,提高模块化,即通过AOP解决代码耦合问题,让职责更加单一。

运用技术:

​ SpringAOP使用了两种代理机制,一种是基于JDK的动态代理,另一种是基于CGLib的动态代理,之所以需要两种代理机制,很大程度上是因为JDK本身只提供基于接口的代理,不支持类的代理。

切面植入的方法:

  1. 编译期织入

  2. 类装载期织入

  3. 动态代理织入---->在运行期为目标类添加增强生成子类的方式,Spring AOP采用动态代理织入切面

流行的框架:

AOP现有两个主要的流行框架,即Spring AOP和Spring+AspectJ

二者的区别:

1、 织入的时期不同

Spring Aop采用的动态织入,而Aspectj是静态织入。静态织入:指在编译时期就织入,即:编译出来的class文件,字节码就已经被织入了。动态织入又分静动两种,静则指织入过程只在第一次调用时执行;动则指根据代码动态运行的中间状态来决定如何操作,每次调用Target的时候都执行。有不清楚的同学,可以自己补下基础的代理知识

2、从使用对象不同

Spring AOP的通知是基于该对象是SpringBean对象才可以,而AspectJ可以在任何Java对象上应用通知。

Spring AOP:如果你想要在通过this对象调用的方法上应用通知,那么你必须使用currentProxy对象,并调用其上的相应方法;于此相似,如果你想要在某对象的方法上应用通知,那么你必须使用与该对象相应的Spring bean

AspectJ:使用AspectJ的一个间接局限是,因为AspectJ通知可以应用于POJO之上,它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知,这有可能导致一个无限循环。

Spring AOP不同于大多数其他AOP框架。Spring AOP的目的并不是为了提供最完整的AOP实现(虽然Spring AOP具有相当的能力);而是为了要帮助解决企业应用中的常见问题,提供一个AOP实现与Spring IOC之间的紧密集成。由于Spring AOP是容易实现的,如果你计划在Spring Beans之上将横切关注点模块化,Spring的这一目标将是要点之一。但同样的目标也可能成为一个限制,如果你用的是普通的Java对象而不是Spring beans,并基于此将横切关注点模块化的话。另一方面,AspectJ可用于基于普通Java对象的模块化,但在实施之前需要良好的关于这个主题的知识。

在决定使用哪种框架实现你的项目之前,有几个要点可以帮助你做出合适的选择(同样适用于其他框架)。

明确你在应用横切关注点(cross-cutting concern)时(例如事物管理、日志或性能评估),需要处理的是Spring beans还是POJO。如果正在开发新的应用,则选择Spring AOP就没有什么阻力。但是如果你正在维护一个现有的应用(该应用并没有使用Spring框架),AspectJ就将是一个自然的选择了。为了详细说明这一点,假如你正在使用Spring AOP,当你想将日志功能作为一个通知(advice)加入到你的应用中,用于追踪程序流程,那么该通知(Advice)就只能应用在Spring beans的连接点(Joinpoint)之上

例子:在appbeans.xml中配置如下的切入点(pointcut),那么当调用myServices bean的service方法时就将应用日志通知(advice)。

<!—Configuration snippet in appbeans.xml --><bean id="myServices" class="com.ashutosh.MyServicesImpl " /><aop:config><aop:aspect id="loggingAspect" ref="logging"><aop:around method="log" pointcut="execution(public * *(..))"/></aop:aspect></aop:config --> // Java file calling service methodApplicationContext beans =newClassPathXmlApplicationContext("appbeans.xml");MyServices myServices = (MyServices) beans.getBean("myServices");myServices.service(); // Logging advice applied here

看一下日志通知将要被应用处的注释,在这里应用程序将记录被调用方法的详细信息。但是,当你在service()方法中调用同一个类中的其他方法时,如果你没有使用代理对象,那么日志通知就不会被应用到这个方法调用上。

例如:

// MyServices service methodpublic void service() {performOperation();// Logging advice not going to apply here}

如果你想要在通过this对象调用的方法上应用通知,那么你必须使用currentProxy对象,并调用其上的相应方法。

// MyServices service methodpublic void service() {// Logging advice going to apply here((MyServices) AopContext.currentProxy()).performOperation();}

于此相似,如果你想要在某对象的方法上应用通知,那么你必须使用与该对象相应的Spring bean。

public void service() {MyObject obj = new MyObject();Obj.performOperation();// Logging advice not going to apply here}

如果你想要应用该通知,那么上述代码必须修改为如下形式。

public void service() {MyObject obj = new MyObject();Obj.performOperation();// Logging advice not going to apply hereApplicationContext beans =newClassPathXmlApplicationContext("appbeans.xml");MyObject obj =(MyObject) beans.getBean("myObject");obj.performOperation()// Logging advice applied here}

于此不同,使用“AspectJ”你可以在任何Java对象上应用通知,而不需要在任何文件中创建或配置任何bean。

另一个需要考虑的因素是,你是希望在编译期间进行织入(weaving),还是编译后(post-compile)或是运行时(run-time)。Spring只支持运行时织入。如果你有多个团队分别开发多个使用Spring编写的模块(导致生成多个jar文件,例如每个模块一个jar文件),并且其中一个团队想要在整个项目中的所有Spring bean(例如,包括已经被其他团队打包了的jar文件)上应用日志通知(在这里日志只是用于加入横切关注点的举例),那么通过配置该团队自己的Spring配置文件就可以轻松做到这一点。之所以可以这样做,就是因为Spring使用的是运行时织入。

<!—Configuration --><bean id="myServices" class="com.ashutosh.MyServicesImpl " /><aop:config><aop:aspect id="loggingAspect" ref="logging"><aop:around method="log" pointcut="execution(public * *(..))"/></aop:aspect></aop:config -->

如果你使用AspectJ想要做到同样的事情,你也许就需要使用acj(AspectJ编译器)重新编译所有的代码并且进行重新打包。否则,你也可以选择使用AspectJ编译后(post-compile)或载入时(load-time)织入。

因为Spring基于代理模式(使用CGLIB),它有一个使用限制,即无法在使用final修饰的bean上应用横切关注点。因为代理需要对Java类进行继承,一旦使用了关键字final,这将是无法做到的。

例如,在Spring bean MyServicesImpl上使用关键字final,并配置一个“execution(public * *(…))”这样的切入点,将导致运行时异常(exception),因为Spring不能为MyServicesImpl生成代理。

// Configuration file<bean id="myServices" class="com.ashutosh.MyServicesImpl" />//Java filepublic final classMyServicesImpl {---}

在这种情况下,你也许会考虑使用AspectJ,其支持编译期织入且不需要生成代理。

于此相似,在static和final方法上应用横切关注点也是无法做到的。因为Spring基于代理模式。如果你在这些方法上配置通知,将导致运行时异常,因为static和final方法是不能被覆盖的。在这种情况下,你也会考虑使用AspectJ,因为其支持编译期织入且不需要生成代理。

你一定希望使用一种易于实现的方式。因为Spring AOP支持注解,在使用@Aspect注解创建和配置方面时将更加方便。而使用AspectJ,你就需要通过.aj文件来创建方面,并且需要使用ajc(Aspect编译器)来编译代码。所以如果你确定之前提到的限制不会成为你的项目的障碍时,使用Spring AOP。

使用AspectJ的一个间接局限是,因为AspectJ通知可以应用于POJO之上,它有可能将通知应用于一个已配置的通知之上。对于一个你没有注意到这方面问题的大范围应用的通知,这有可能导致一个无限循环。

例如,创建一个包含如下切入点的方面。

public aspectLogging {Object around() : execution(public * * (..))Sysytem.out.println(thisJoinPoint.getSignature());return proceed();}

在这种情况下,当proceed即将被调用时,日志通知会被再次应用,这样就导致了嵌套循环。

所以,如果你希望在Spring bean上采取比较简单的方式应用横切关注点时,并且这些bean没有被标以final修饰符,同时相似的方法也没有标以static或final修饰符时,就使用Spring AOP吧。相比之下,如果你需要在所提到的限制之上应用横切关注点,或者要在POJO上应用关注点,那么就使用AspectJ。你也可能选择同时使用两种方法,因为Spring支持这样。

参考链接:http://docs.spring.io/spring/docs/3.0.x/reference/aop.html

http://www.oschina.net/translate/comparative_analysis_between_spring_aop_and_aspectj?cmp

SpringAop与AspectJ的联系与区别____比较分析 Spring AOP 和 AspectJ 之间的差别相关推荐

  1. 比较分析 Spring AOP 和 AspectJ 之间的差别

    面向方面的编程(AOP) 是一种编程范式,旨在通过允许横切关注点的分离,提高模块化.AOP提供方面来将跨越对象关注点模块化.虽然现在可以获得许多AOP框架,但在这里我们要区分的只有两个流行的框架:Sp ...

  2. Spring AOP 和 AspectJ的区别

    Spring AOP 和 AspectJ的区别 springAOP 是spring支持的面向切面AOP 编程. AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法 ...

  3. aopaspect区别_面试官:什么是AOP?Spring AOP和AspectJ的区别是什么?

    AOP(Aspect Orient Programming),它是面向对象编程的一种补充,主要应用于处理一些具有横切性质的系统级服务,如日志收集.事务管理.安全检查.缓存.对象池管理等. AOP实现的 ...

  4. 框架源码专题:Spring的Aop实现原理,Spring AOP 与 AspectJ 的关系

    文章目录 1. Spring AOP 与 AspectJ 的关系 2. JDK和Cglib动态代理的区别 3. Spring AOP应用案例 4. Spring AOP有几种配置方式? 5. Spri ...

  5. 比较Spring AOP与AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ 介绍 如今有多个可用的AOP库,这些组件需要回答一系列的问题: 是否与我现有的应用兼容? 我在哪实现AOP? 集成到我的 ...

  6. 在 Spring Boot 中使用 Spring AOP 和 AspectJ 来测量方法的执行时间

    原文链接:https://dzone.com/articles/logging-average-method-execution-times-via-aspectj 作者:Murat Derman 译 ...

  7. 比较Spring AOP和AspectJ

    1. 介绍 当前有多个可用的AOP库,这些库必须能够回答许多问题: 它与我现有的或新的应用程序兼容吗? 在哪里可以实施AOP? 它与我的应用程序集成的速度有多快? 性能开销是多少? 在本文中,我们将着 ...

  8. Spring AOP and AspectJ AOP 有什么区别

    AOP实现的关键在于 代理模式,AOP代理主要分为静态代理和动态代理.静态代理的代表为AspectJ:动态代理则以Spring AOP为代表. (1)AspectJ是静态代理的增强,所谓静态代理,就是 ...

  9. 面试官:说说Spring AOP、AspectJ、CGLIB ?它们有什么关系?

    欢迎关注方志朋的博客,回复"666"获面试宝典 AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务 ...

最新文章

  1. 解决事件多次绑定,执行多次问题
  2. SAP HUM 没有搬到Storage Type 923的HU能用HU02拆包?
  3. sift计算描述子代码详解_代码详解——如何计算横向误差?
  4. 如何在64位win7下通过ODAC来访问Oracle服务器
  5. 1^2+2^2+……+n^2的公式证明
  6. 开发小计之判断输入字符串类型(正则表达式)
  7. VS恢复调试时出现的确认对话框
  8. python中map和filter区别_Python中map、filter和reduce的使用总结
  9. LAMP之PHP服务的安装(libphp7.so方式)
  10. MAC上DOS常用命令操作
  11. 微软雅黑在IE中显示为宋体
  12. 【Neo4j】第 1 章:图数据库
  13. java十大排序法_Java 十大排序算法
  14. QQ服务器Ip用于文件传输,QQ文件传输协议
  15. 《走出幻觉,走向成熟》--读书笔记1
  16. 苹果电脑安装双系统Mac和Win7,详细教程
  17. 【华为云-IP资源冻结机制】华为云安全事故冻结服务器IP近1小时【50分钟系统全线不可用】
  18. C++ 拉格朗日插值法优化 DP
  19. 曾做erp开发工程师,谈下自己的经验
  20. 解决办法: Vue cross-env NODE_ENV=production webpack --progress --hide-module

热门文章

  1. combobox控件 如何把三角形放大_初中数学|全等三角形全部知识点总结
  2. [转载] java中关于用\t格式输出
  3. inputstream示例_Java InputStream close()方法与示例
  4. Java PipedInputStream connect()方法与示例
  5. fedora操作系统优缺点_不同类型的操作系统的优缺点
  6. ruby hash方法_Hash.fetch()方法以及Ruby中的示例
  7. 简易的遍历文件加密解密
  8. turbo c相关文档
  9. Python-Pandas之两个Dataframe的差异比较
  10. 2s相机 android6,Android Camera2 使用总结