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,在执行被'关注'的方法时,'切入'的方法就会被执行.
一个完整的例子:
需要被'切入'的类:

[java] view plaincopy
  1. public class Monkey {
  2. public void stealPeaches(String name) {
  3. System.out.println(" Monkey " + name + " is stealling peaches...");
  4. }
  5. public void stealCorns(String name) {
  6. System.out.println(" Monkey " + name + " is stealling corns...");
  7. }
  8. }

'切面'类:

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

配置文件:

[html] view plaincopy
  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. <bean id="monkey" class="com.test.spring.aspectj.Monkey" />
  17. </beans>

使用bean:

[java] view plaincopy
  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. }

运行结果:

[plain] view plaincopy
  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类:

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

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

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

XML配置:

[html] view plaincopy
  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. <bean id="guardian" class="com.test.spring.aspectj.XMLGuardian" />
  10. <bean id="monkey" class="com.test.spring.aspectj.Monkey" />
  11. <aop:config>
  12. <aop:aspect id="myAspect" ref="guardian">
  13. <aop:pointcut id="guardOrchard"
  14. expression="execution(* com.test.spring.aspectj.Monkey.stealPeaches(..))" />
  15. <aop:before pointcut-ref="guardOrchard" method="guardOrchardBefore" />
  16. <aop:after-returning pointcut-ref="guardOrchard" method="guardOrchardAfter"/>
  17. <aop:pointcut id="guardFarm"
  18. expression="execution(* com.test.spring.aspectj.Monkey.stealCorns(..))" />
  19. <aop:before pointcut-ref="guardFarm" method="guardFarmBefore" />
  20. <aop:after-returning pointcut="execution(* com.test.spring.aspectj.Monkey.stealCorns(..)) and args(name,num,..)" returning="retVal"
  21. method="guardFarmAfter" />
  22. <!--  arg-names="name1" -->
  23. </aop:aspect>
  24. </aop:config>
  25. </beans>

客户端测试代码:

[java] view plaincopy
  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. }

运行结果:

[plain] view plaincopy
  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. 如何理解 Spring AOP 以及使用 AspectJ?

    作者 | 阿文 责编 | 屠敏 出品 | CSDN(ID:CSDNnews) 在 Spring 中 AOP 是一个非常非常重要的概念,那么什么是AOP呢? AOP 即面向切面编程,也可以叫做面向方向编 ...

  2. 14.spring aop之aspect切入点语法详解

    1.Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的,,在Spring AOP中目前只有执行方法这一个连接点,Spring AOP支持的AspectJ切入点指 ...

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

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

  4. 面试官:Spring AOP、AspectJ、CGLIB 都是什么鬼?它们有什么关系?

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等. AOP 实现的关键就在 ...

  5. Spring AOP,AspectJ,CGLIB 有点晕

    AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理.安全检查.缓存.对象池管理等.AOP 实现的关键就在于 ...

  6. SpringAop与AspectJ的联系与区别____比较分析 Spring AOP 和 AspectJ 之间的差别

    SpringAop与AspectJ的联系与区别 区别 AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Ja ...

  7. Spring AOP技术(基于AspectJ)的XML开发

    Spring AOP技术(基于AspectJ)的XML开发 @(Spring)[aop, spring, xml, Spring, annotation, aspectJ] Spring AOP技术基 ...

  8. 比较Spring AOP与AspectJ

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

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

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

最新文章

  1. pycharm安装tensorflow
  2. java解析kafkaavro_如何使用Spring Kafka读取合并模式注册的AVRO消息?
  3. 上海交大:基于近似随机Dropout的LSTM训练加速
  4. 新CCIE笔记之'口口相传'路由协议
  5. 你的接口能承受高并发吗?
  6. Spring Cloud(F版)搭建高可用服务注册中心
  7. 前端学习(527):等分布局第二种方案
  8. RabbitMQ配置文件
  9. 8、信息隐藏技术:数字水印技术
  10. Comdo安全防火墙
  11. 小学计算机网络教室管理制度,计算机网络教室使用管理制度[大全五篇]
  12. DCDC开关电源的阶跃响应和动态响应(Load Transient)的区别
  13. 计算机键盘fn,笔记本键盘fn键有什么用 笔记本键盘fn键使用说明大全
  14. python pillow库画图_Pillow画图
  15. 把Redis当作队列来用,真的合适吗
  16. 一个优秀IT专家的成长历程---献给所有的颓废或即将颓废的人们
  17. S32K14x CAN休眠唤醒的实现方案
  18. 什么是性能测试?性能测试目的?性能测试的主要分类以及性能测试的常用指标?
  19. 三西格玛和六西格玛区别是什么?优思学院用一幅图告诉你
  20. 游戏开发30课 cocoscreator骨骼贴图布局设置

热门文章

  1. 表白网页自助申请系统
  2. 深入实践 ES6 Proxy Reflect
  3. 如何搭建一个自己的音乐服务器
  4. UltraEdit小众用法
  5. 头文件intrins.h的用法
  6. 英语SouthRedAgate南红玛瑙southredagate单词
  7. 一次网络世界的旅行-简单理解网络通信
  8. docker_相关操作
  9. 6-7 使用函数求素数和 (20 分)
  10. 典型2R机械臂结构分析 2R-manipulator Geometric Modeling