这一主要看看Spring AOP是如何实现通知包围的。
 
Spring AOP包围通知在功能上和前置通知加后置通知类似,但还是有区别的:包围通知可以修改返回值,还可以阻止、替换目标方法的执行。
 
Spring里的包围通知是实现MethodInterceptor接口的拦截器。
 
Spring包围通知有着很广泛的应用,比如远程代理和事务管理,都是由拦截器完成。另外,拦截器也是剖析程序运行的好方法。
 
下面利用Spring AOP包围通知实现监控业务方法的执行运行过程耗时情况。
 
/** 
* 业务组件 
*/ 
public class WorkerBean {

public void doSomeWork(int noOfTimes) { 
        for(int x = 0; x < noOfTimes; x++) { 
            work(); 
        } 
    } 
     
    private void work() { 
        System.out.print(""); 
    } 
}

 
import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
import org.springframework.util.StopWatch;

/** 
* 拦截器,实现方法包围通知 
*/ 
public class ProfilingInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable { 
        //启动一个 stop watch 
        StopWatch sw = new StopWatch(); 
        //运行计时器 
        sw.start(invocation.getMethod().getName()); 
        //执行业务方法 
        Object returnValue = invocation.proceed(); 
        //停止计时器 
        sw.stop(); 
        //垃圾信息输出 
        dumpInfo(invocation, sw.getTotalTimeMillis()); 
        //返回业务方法返回值 
        return returnValue; 
    }

/** 
     * 垃圾信息输入方法,实际上输出的是方法运行的计时信息 
     */ 
    private void dumpInfo(MethodInvocation invocation, long ms) { 
        //获取被调用方法 
        Method m = invocation.getMethod(); 
        //获取被调用方法所属的对象 
        Object target = invocation.getThis(); 
        //获取被调用方法的参数 
        Object[] args = invocation.getArguments();

System.out.println("所执行的方法: " + m.getName()); 
        System.out.println("对象的类型: " + target.getClass().getName());

System.out.println("方法的参数:"); 
        for (int x = 0; x < args.length; x++) { 
            System.out.print("    > " + args[x]); 
        } 
        System.out.print("\n");

System.out.println("抓取方法运行的时间: " + ms + " ms"); 
    } 
}

 
import org.springframework.aop.framework.ProxyFactory;

/** 
* 客户端测试方法 
*/ 
public class ProfilingExample {

public static void main(String[] args) { 
        //创建代理对象 
        WorkerBean bean = getWorkerBean(); 
        //在代理对象上调用业务方法 
        bean.doSomeWork(10000000); 
    }

/** 
     * 代理对象工厂 
     */ 
    private static WorkerBean getWorkerBean() { 
        //创建目标对象 
        WorkerBean target = new WorkerBean(); 
        //构建代理对象工厂 
        ProxyFactory factory = new ProxyFactory(); 
        factory.setTarget(target); 
        factory.addAdvice(new ProfilingInterceptor());

//生产一个代理对象 
        return (WorkerBean)factory.getProxy(); 
    } 
}

 
运行结果:
- Using JDK 1.4 collections 
所执行的方法: doSomeWork 
对象的类型: com.apress.prospring.ch6.profiling.WorkerBean 
方法的参数: 
    > 10000000 
抓取方法运行的时间: 3453 ms

Process finished with exit code 0

 
从输出的结果中,可以看到程序方法调用的方法名、参数、所属类,以及执行方法所耗费的时间。
 
另外说一下org.springframework.util.StopWatch类,这是个计时器类,此工具类主要可以获取计时器类start()和stop()两次方法调用间的时间。具体可以查看Spring的API文档。另外,我也在apache commons 包里面也有org.apache.common.lang.time.StopWatch。

http://lavasoft.blog.51cto.com/62575/75342/

Spring AOP 本质(4)的更多相关文章

  1. Spring AOP就是这么简单啦

    前言 只有光头才能变强 上一篇已经讲解了Spring IOC知识点一网打尽!,这篇主要是讲解Spring的AOP模块~ 之前我已经写过一篇关于AOP的文章了,那篇把比较重要的知识点都讲解过了一篇啦:S ...

  2. Spring AOP学习笔记01:AOP概述

    1. AOP概述 软件开发一直在寻求更加高效.更易维护甚至更易扩展的方式.为了提高开发效率,我们对开发使用的语言进行抽象,走过了从汇编时代到现在各种高级语言繁盛之时期:为了便于维护和扩展,我们对某些相 ...

  3. Spring AOP技术本质认识

    Spring AOP技术本质认识 一.AOP简介   AOP(Aspect Oriented Programming,面向切面编程),把某一类问题集中在一个地方进行处理,比如处理程序中的点击事件.打印 ...

  4. [转]彻底征服 Spring AOP 之 理论篇

    基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...

  5. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  6. Spring AOP整理

    示例展示 AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充.AOP之所以能得到广泛认可,主要是因为它将应用系统拆分分了 ...

  7. Spring Aop 应用实例与设计浅析

    0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...

  8. JavaEE学习之Spring AOP

    一.基本概念 AOP——Aspect-Oriented Programming,面向切面编程,它是spring框架的一个重要组成部分.一般的业务逻辑都有先后关系,我们可以理解为纵向关系,而AOP关注的 ...

  9. Spring AOP的实现研究

    1. 背景 在前文Spring IOC容器创建bean过程浅析已经介绍了Spring IOC创建初始化bean的大致过程.现在对Spring的AOP实现机制进行研究分析. 2. 名词与概念 名词 概念 ...

随机推荐

  1. EXTJS4:在grid中加入合计行

    extjs4很方便的实现简单的合计(针对在不分页的情况下): 它效果实现在:Ext.grid.feature.Summary这个类中 Ext.define('TestResult', { extend ...

  2. svn由于连接方在一段时间后没有正确答复或连接的主机没有反应连接尝试失败

    解决方法,关掉防火墙, service iptables status 查看iptables状态 service iptables restart iptables服务重启 service iptab ...

  3. 【转】Ubuntu重装,直接进win7,不进linux的解决方案(添加Ubuntu启动菜单)

    原文网址:http://my.oschina.net/u/1377657/blog/281875 本人重装了Ubuntu12.04.4 LTS, 将Ubuntu启动项放在了/boot分区里面,启动的时 ...

  4. php+jquery+ajax+json的一个最简单实例

    html页面: <html> <head> <meta http-equiv="content-type" content="text/ht ...

  5. poj:2992 因子数量

    题意: 求 组合数c(n,k)的因子数量 由算术基本定理很容易求得,不过第一次却T了,加了好多预处理,o1查询,才过 #include <iostream> #include <st ...

  6. MySql按日期时间段进行统计(前一天、本周、某一天、某个时间段)

    在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time 统计当天 sql语句为: select * from user where date(log_tim ...

  7. pyqt node节点1

    #!/usr/bin/env python # coding: utf-8 from PyQt4.QtGui import * from PyQt4.QtCore import * rad = 5 c ...

  8. [MongoDB] Remove, update, create document

    Remove: remove the wand with the name of "Doom Bringer" from our wandscollection. db.wands ...

  9. C++11下的线程池以及灵活的functional + bind + lamda

    利用boost的thread实现一个线程类,维护一个任务队列,以便可以承载非常灵活的调用.这个线程类可以方便的为后面的线程池打好基础.线程池还是动态均衡,没有什么别的.由于minGW 4.7 对 C+ ...

  10. SQL函数简述

    数字函数ABS 取绝对值 POWER 乘方 LN 10为底数取幂SQRT 平方根 EXP e的n次乘方 LOG(m,n) m为底数n取幂数学运算函数:ACOS ATAN ATAN2 COS COSH ...