【Spring实战】—— 9 AOP环绕通知
2016-04-15 12:04
441 查看
假如有这么一个场景,需要统计某个方法执行的时间,如何做呢?
典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间。
如果采用Spring的AOP,仅仅使用前置和后置方法是无法做到的,因为他们无法共享变量。这样通过环绕通知,就可以快捷的实现。
首先在切面通知类中声明环绕通知类:
在bean.xml配置文件中配置aop:around,锁定方法:
这样执行的结果如下:
因此可以看出AOP执行的过程如下:
典型的会想到在方法执行前记录时间,方法执行后再次记录,得出运行的时间。
如果采用Spring的AOP,仅仅使用前置和后置方法是无法做到的,因为他们无法共享变量。这样通过环绕通知,就可以快捷的实现。
首先在切面通知类中声明环绕通知类:
public void watchPerformance(ProceedingJoinPoint joinpoint){ try{ System.out.println("begin!"); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("end! performance took "+(end-start)+" milliseconds"); }catch(Throwable e){ System.out.println("eee!We want our money back!"); } }
在bean.xml配置文件中配置aop:around,锁定方法:
<aop:around pointcut-ref="performance" method="watchPerformance"/>
这样执行的结果如下:
The audience is taking their seats. The audience is turning off their cellphones begin! Instrumentalist age:25 Playing Jingle Bells:TOOT TOOT TOOT CLAP CLAP CLAP end! performance took 95 milliseconds
因此可以看出AOP执行的过程如下:
before() around() 执行方法() after/throw() around()
相关文章推荐
- Open JDK 建立SSL失败的问题
- 转:如何查看MyEclipse包含的Eclipse的版本号
- java中解决构造函数中成员变量和形参重名的问题
- java实现栈
- spring4.2+spring mvc +spring data+jpa+hibernate的程序构架
- java中静态代码块的用法 static用法详解
- 转:myeclipse和eclipse的区别和联系,以及版本间的对应关系
- springmvc向页面传值时对list去重
- 转: MyEclipse 10.0,9.0,8.0 下添加jadClipse反编译插件
- eclipse常用20个快捷键
- Java并发编程:Lock
- spring aop中的propagation的7种配置的意思
- 基于配置的Spring AOP
- Editplus如何进行JAVA程序编译调试
- java生成图片验证码实例代码
- SpringMVC、MyBatis声明式事务管理+AOP配置
- Java 二分查找
- 开发环境配置-在eclipse中关联jar的源码和javadoc-9
- springmvc实现文件上传下载
- myEclipse中设置代码区的背景颜色