主要利用了Spring AOP 技术,对想要统计的方法进行横切处理,方法执行前开始计时,方法执行后停止计时,得到计时方法就是该方法本次消耗时间。

步骤:

  • 首先编写自己的Interceptor类来实现MethodInterceptor类,来用于切入方法,运行计时代码
  • Spring AOP 的XML配置,配置需要监测的方法和切入方法(自定义的Interceptor)

1、自定义Intercept拦截器

package com.utis.intercept;

import java.util.HashMap;
import java.util.Map; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.time.StopWatch;
/**
* 方法运行时间测试
* @author Saiteam
*
*/
public class MethodTimeActive implements MethodInterceptor { /*
* 自定义map集合,key:方法名,value:[0,运行次数,1:总时间]
*/
public static Map<String, Long[]> methodMap = new HashMap<String, Long[]>(); /*
* 拦截要执行的方法
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("MethodTimeActive.invoke()");
//1、创建一个计时器
StopWatch watch = new StopWatch();
//2、计时器开始
watch.start();
//3、执行方法
Object object = invocation.proceed();
//4、计时器停止
watch.stop();
//5、获取方法名称
String methodName = invocation.getMethod().getName();
//6、获取耗时多少
Long time = watch.getTime();
//7、判断方法执行了多少次,耗时多少
if(methodMap.containsKey(methodName)){
Long[] x = methodMap.get(methodName);
x[0]++;
x[1] +=time;
}else{
methodMap.put(methodName, new Long[]{1L,time});
}
return object;
} }

2、配置applicationContext.xml文件,利用AOP横向切面编程

	<aop:config>
<aop:pointcut id="baseServiceMethods" expression="execution(* com.booksys.service.*.*(..))" />
<aop:advisor advice-ref="methodTimeActive" pointcut-ref="baseServiceMethods" />
</aop:config> <bean id="methodTimeActive" class="com.utis.intercept.MethodTimeActive"></bean>

3、单元测试

package SSITest;

import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.booksys.service.BookService;
import com.utis.intercept.MethodTimeActive; public class MethodInterTest { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testMethodTime(){
BookService bookService = (BookService) context.getBean("bookService");
System.out.println(bookService.findBookById(1).getBookname());
}      //----------------重要的是这个-------------------------
@After
public void testMethodActive(){
Map<String, Long[]> map = MethodTimeActive.methodMap;
Set<String> set = map.keySet();
Long[] x = null;
for(String s : set){
x = map.get(s);
System.out.println(s+":"+x[0]+"次,"+x[1]+"毫秒");
}
} }

测试结果:

  MethodTimeActive.invoke()
  11:46:20,912 DEBUG Connection:27 - {conn-100000} Connection
  11:46:20,922 DEBUG Connection:27 - {conn-100000} Preparing Statement:     select * from book where bookid=?   
  java基础教程
  312
  findBookById:1次,312毫秒

spring3.0框架检测方法运行时间测试(转)的更多相关文章

  1. java监测方法运行时间/效率方法

    前言: 这周在写一个小项目,虽然小但是是纯调外部接口的,调完了接口还不停的循环接口返回的数据(已转换JSONArray),然后再判断值,再做不同处理,关键是数据量还比较大,这刚做完还没开始上线,测试也 ...

  2. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  3. 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

    一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...

  4. SSH框架(四) struts2+spring3.0的登陆示例

    (一)关键理念及需要注意的地方: 使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baid ...

  5. SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置

    一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...

  6. 安卓4.0以上系统怎么不用root激活XPOSED框架的方法

    在大多单位的引流或业务操作中,基本上都需要使用安卓的高端技术Xposed框架,近期,我们单位购买了一批新的安卓4.0以上系统,基本上都都是基于7.0以上版本,基本上都不能够刷入root超级权限,即便是 ...

  7. Fundebug前端JavaScript插件更新至1.6.0,新增test()方法用于测试

    摘要: 1.6.0新增fundebug.test()方法用于测试,请大家及时更新. 默认情况下,Fundebug 插件能够自动捕获未处理的错误(uncaught error).另外,开发者也可以通过使 ...

  8. 测试JS方法运行时间

    console.time(label) 和 console.timeEnd(label), 在开始的地方写上 console.time("测试 fn 速度: ") ,在结束的地方写 ...

  9. thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法

    这篇文章主要介绍了thinkPHP5.0框架验证码调用及点击图片刷新简单实现方法,结合简单示例形式分析了thinkPHP5框架验证码相关配置.后台验证.前台刷新等操作技巧,学习thinkphp源码的朋 ...

随机推荐

  1. 虚方法(virtual)和抽象方法(abstract)的和接口(interface)的区别

    虚方法(virtual)和抽象方法(abstract)的区别 2017年06月15日 13:41:26 阅读数:65 注:本文转载自 http://www.cnblogs.com/michaelxu/ ...

  2. 移动端常见bug

    meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=device-wid ...

  3. Python Day 8

    阅读目录:   内容回顾 三种字符串 文件操作三步骤 基础的读 基础的写 with...open()语法 文件的操作模式 文件的操作编码问题 文件的复制 游标操作 ##内容回顾 类型转换 #1.数字类 ...

  4. mktime 夏令时

    我们的最终目的是把字符串格式的时间转换为内部使用的“日历时间”,即到UTC 1970年1月1日零时的秒数.这里就存在夏令时的问题.比如俄罗斯时间2008年10月26日2:30由于夏令时的跳变会经过两次 ...

  5. IOS 设置视图半透明子控件不透明

    代码处理: UIColor *color = [[UIColor blackColor] colorWithAlphaComponent:0.6]; self.view.backgroundColor ...

  6. git撤销commit-hard

    场景: 不小心commit了一个不应该commit的修改,但是还没有push,想撤销那个commit 命令: a)git log b)git reset --hard commit_id 具体步骤如下 ...

  7. PIO学习

    边沿捕获 PIO可以对输入进行边沿捕获,它可以捕获上升沿.下降沿和双沿,当检测到边沿时PIO会把它存在edgecapture 寄存器之内: 打开Synchronously capture 时,会生成一 ...

  8. 效果监控js源码

    function _bxmPlatformFn(e, t) { var n, o, i = ""; try { i = localStorage.getItem("lis ...

  9. 基于UML的时空建模

    一.基本信息 标题:基于UML的时空建模 时间:2018 出版源:东北大学学报(自然科学版) 领域分类:UML模型:RCC-8空间拓扑:Allen-13时态拓扑:时空数据:建模 二.研究背景 问题定义 ...

  10. SQL数据库约束、默认和规则

    数据的完整性 实体完整性 又称为行完整性,即数据库中的所有行都具有一个非空且没有重复的主键值 MSSQL中通过唯一索引.PRIMARY KEY约束.UNIQUE约束.INDENTITY属性等来强制主键 ...