Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ.
一:使用AspectJ注解:
1,启用对AspectJ的支持:
通过在Spring的配置中引入下列元素来启用Spring对AspectJ的支持:
<aop:aspectj-autoproxy />
或者(如果不是使用XSD的话)
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
2,声明一个带有@Aspect注解的类,在这个类中声明那些方法需要被'关注'(利用@Pointcut),在那些时机点进行关注(利用@Before,@AfterReturning等等...),执行'切入'的方法
3,在Spring的配置文件中定义这个'切面'类:任意带有一个@Aspect切面(拥有@Aspect注解)的bean都将被Spring自动识别并用于配置在Spring AOP.
4,使用被Spring管理的bean,在执行被'关注'的方法时,'切入'的方法就会被执行.
一个完整的例子:
需要被'切入'的类:

  1. public class Monkey {
  2. public void stealPeaches(String name) {
  3. System.out.println(" Monkey " + name + " is stealling peaches...");
  4. }
  5.  
  6. public void stealCorns(String name) {
  7. System.out.println(" Monkey " + name + " is stealling corns...");
  8. }
  9. }
  1. 切面类
    1 @Aspect
  2. public class Guardian {
  3. @Pointcut("execution(* com.test.spring.aspectj.Monkey.stealPeaches(..))")
  4. public void guardOrchard() {
  5. }
  6.  
  7. @Before(value = "guardOrchard()")
  8. public void guardOrchardBefore() {
  9. System.out.println("Guardian spotted a monkey is approaching the orchard...");
  10. }
  11.  
  12. @AfterReturning("guardOrchard() && args(name,..)")
  13. public void guardOrchardAfter(String name) {
  14. System.out.println("Guardian caught a monkey stealling peaches whoes name is " + name + "...");
  15. }
  16. @Around("guardOrchard() && args(name,..)")
  17. public void guardOrchardAround(ProceedingJoinPoint joinpoint,String name) {
  18. System.out.println("Guardian guardOrchardAround started ... " + name);
  19. try {
  20. joinpoint.proceed();
  21. } catch (Throwable e) {
  22. System.out.println("Guardian guardOrchardAround exception happened ... " + name);
  23. }
  24. System.out.println("Guardian guardOrchardAround completed ... " + name);
  25. }
  26. @Pointcut("execution(* com.test.spring.aspectj.Monkey.stealCorns(..))")
  27. public void guardFarm() {
  28. }
  29.  
  30. @Before(value = "guardFarm()")
  31. public void guardFarmBefore() {
  32. System.out.println("Guardian spotted a monkey is approaching the farm...");
  33. }
  34.  
  35. @AfterReturning("guardFarm() && args(name,..)")
  36. public void guardFarmAfter(String name) {
  37. System.out.println("Guardian caught a monkey stealling corns whoes name is " + name + "...");
  38. }
  39. }

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  9. <!--
  10. <aop:aspectj-autoproxy /> equals to <bean
  11. class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"
  12. />
  13. -->
  14. <aop:aspectj-autoproxy />
  15. <bean id="guardian" class="com.test.spring.aspectj.Guardian" />
  16.  
  17. <bean id="monkey" class="com.test.spring.aspectj.Monkey" />
  18.  
  19. </beans>

使用bean

  1. ApplicationContext context = new ClassPathXmlApplicationContext("conf/aspectJAppcontext.xml");
  2. Monkey monkey = (Monkey) context.getBean("monkey");
  3. try {
  4. monkey.stealPeaches("mighty monkey");
  5. monkey.stealCorns("mighty monkey");
  6. } catch (Exception e) {
  7. }

运行结果

  1. Guardian spotted a monkey is approaching the orchard...
  2. Guardian guardOrchardAround started ... mighty monkey
  3. Monkey mighty monkey is stealling peaches...
  4. Guardian caught a monkey stealling peaches whoes name is mighty monkey...
  5. Guardian guardOrchardAround completed ... mighty monkey
  6. Guardian spotted a monkey is approaching the farm...
  7. Monkey mighty monkey is stealling corns...
  8. Guardian caught a monkey stealling corns whoes name is mighty monkey...

二:通过XML配置AspectJ实现AOP:在java类中定义要被'方面'调用的切入方法,在XML中配置.
例子:被'切入'的类,普通java类:

  1. public class Monkey {
  2. public void stealPeaches(String name) {
  3. System.out.println(" Monkey " + name + " is stealling peaches...");
  4. }
  5.  
  6. public void stealCorns(String name,int numberToSteal) {
  7. System.out.println(" Monkey " + name + " is stealling corns...");
  8. }
  9. }

定义要被'方面'调用的切入方法的类:

  1. public class XMLGuardian {
  2. public void guardOrchardBefore() {
  3. System.out.println("XMLGuardian spotted a monkey is approaching the orchard...");
  4. }
  5.  
  6. public void guardOrchardAfter() {
  7. System.out.println("XMLGuardian caught a monkey stealling peaches whoes name is ...");
  8. }
  9. public void guardFarmBefore() {
  10. System.out.println("XMLGuardian spotted a monkey is approaching the farm...");
  11. }
  12. public void guardFarmAfter(String name,int num,Object retVal) {
  13. System.out.println("XMLGuardian caught a monkey stealling " + num + " corns whoes name is ..." + name );
  14. }
  15. }

XML配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  9.  
  10. <bean id="guardian" class="com.test.spring.aspectj.XMLGuardian" />
  11. <bean id="monkey" class="com.test.spring.aspectj.Monkey" />
  12. <aop:config>
  13.  
  14. <aop:aspect id="myAspect" ref="guardian">
  15. <aop:pointcut id="guardOrchard"
  16. expression="execution(* com.test.spring.aspectj.Monkey.stealPeaches(..))" />
  17.  
  18. <aop:before pointcut-ref="guardOrchard" method="guardOrchardBefore" />
  19. <aop:after-returning pointcut-ref="guardOrchard" method="guardOrchardAfter"/>
  20.  
  21. <aop:pointcut id="guardFarm"
  22. expression="execution(* com.test.spring.aspectj.Monkey.stealCorns(..))" />
  23.  
  24. <aop:before pointcut-ref="guardFarm" method="guardFarmBefore" />
  25. <aop:after-returning pointcut="execution(* com.test.spring.aspectj.Monkey.stealCorns(..)) and args(name,num,..)" returning="retVal"
  26. method="guardFarmAfter" />
  27. <!-- arg-names="name1" -->
  28. </aop:aspect>
  29. </aop:config>
  30. </beans>

客户端测试代码:

  1. ApplicationContext context = new ClassPathXmlApplicationContext("conf/xmlaspectJAppcontext.xml");
  2. Monkey monkey = (Monkey) context.getBean("monkey");
  3. try {
  4. monkey.stealPeaches("mighty monkey");
  5. monkey.stealCorns("mighty monkey",3);
  6. } catch (Exception e) {
  7. }

运行结果

  1. XMLGuardian spotted a monkey is approaching the orchard...
  2. Monkey mighty monkey is stealling peaches...
  3. XMLGuardian caught a monkey stealling peaches whoes name is ...
  4. XMLGuardian spotted a monkey is approaching the farm...
  5. Monkey mighty monkey is stealling corns...
  6. XMLGuardian caught a monkey stealling 3 corns whoes name is ...mighty monkey

Spring AOP 只支持对bean的方法级的'切入',而且AOP的内部机制和AspectJ有所区别,Spring主要是通过动态代理来实现AOP,使用JDK的动态代理(如果被代理的bean是interface的话)或者CGLIB(如果如果被代理的bean不是interface的话).

(转)实例简述Spring AOP之间对AspectJ语法的支持(转)的更多相关文章

  1. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  2. spring AOP 之四:@AspectJ切入点标识符语法详解

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  3. Spring Aop中execution的语法

    参考地址:https://blog.csdn.net/zz210891470/article/details/54175107 execution(* com.sample.service.impl. ...

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

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

  5. Spring AOP应用实例demo

    AOP(Aspect-Oriented Programming.面向方面编程).能够说是OOP(Object-OrientedPrograming.面向对象编程)的补充和完好.OOP引入封装.继承和多 ...

  6. Comparing Spring AOP and AspectJ

    AOP 概念 在我们开始之前 , 让我们做一个快速.高级别审查的核心术语和概念 : 方面 — —标准 / 特征代码被分散在多个场所中的应用 , 通常不同于实际的业务逻辑 (例如 , 交易管理) .各方 ...

  7. 比较 Spring AOP 与 AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ(转载:https://juejin.im/post/5a695b3cf265da3e47449471) 介绍 如今有多个 ...

  8. Spring AOP 和 AspectJ

    现如今有许多个可用的 AOP 库,使用这些库需要能够回答以下问题: 是否与现有的或新的应用程序兼容? 在哪里可以使用 AOP ? 如何迅速与应用程序集成? 性能开销是多少? 在本文中,我们将回答这些问 ...

  9. spring AOP 之三:使用@AspectJ定义切入点

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

随机推荐

  1. VBA唏嘘戏——简单单元格的设定(实例)

    由于有很多个Word文件,所以应用宏会更加方便排版,而且版式较为统一. Sub 设置列宽() ' ' 设置列宽宏 ' ' ActiveDocument.Tables().Cell(, ).Width ...

  2. 配置 vim

    cd / vim /etc/vim/vimrc 1.Ubuntu vim显示行号 在文件末端添加一新行,输入 set nu 2.Ubuntu vim语法高亮 在文件中找到 "syntax o ...

  3. MFC-01-Chapter01:Hello,MFC---1.2 MFC简介

    1.2 MFC简介 MFC是Microsoft提供的放置Windows API的面向对象的包装的C++类库.MFC大约封装了好几百个类,其中有一些可以直接调用,有些类可以作为用户自己的类的基类.一些M ...

  4. 第三篇bootstrap 网格基础

    Bootstrap 提供了一套响应式.移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. 网格系统类似一个表格,有行和列,它必须放置在一个类型设置为c ...

  5. as3 代码加解密

    private var loader:URLLoader; ... private function init():void { loader = new URLLoader; req=URLRequ ...

  6. google closure继承模块三:goog.base()源码分析

    直接看代码吧: base: function (me, opt_methodName, var_args) { var caller = arguments.callee.caller; if (ca ...

  7. Oracle中SQL查询表字段基本信息、主键、外键(转)

    select utc.column_name, utc.data_type, utc.data_length, utc.data_precision, utc.data_Scale, utc.null ...

  8. 作业8 Alpha阶段项目总结

    我们的扫雷游戏已经基本完成. 游戏共分3个难度 每个难度的格数和雷的格数也有不同 具体的游戏会在展示时候让大家看到 小组成员分数: 史劭聪 20分 马浩然 20分

  9. B2C电子商务基础系统架构解析(转载)

    系统的开发与演化,前台严格follow消费者的购买流程,后台则盯牢订单流转,牢牢抓住这两条主线,才能高屋建瓴的看清B2C的逻辑链和数据流,更深刻的规划功能模块,从而更有效支撑实际业务的流转. 前台 前 ...

  10. SpellTime

    如果你的应用程序允许用户输入文本,或者它结合了任何基于文本的处理,那么我们有一款你一直寻找的产品.Spell Time 允许你把个拼写检查器整合到你的产品中.该产品携带了完整的源码.Spell Tim ...