主要利用了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. 2019.03.25 bzoj4572: [Scoi2016]围棋(轮廓线dp)

    传送门 题解可以参见zjjzjjzjj神仙的,写的很清楚. 代码: #include<bits/stdc++.h> #define ri register int using namesp ...

  2. String常用类

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  3. 16.The Effect of Advertisement 广告的影响

    16.The Effect of Advertisement 广告的影响 (1) The appeal of advertising to buying motives can have both n ...

  4. 摘录<奇特的一生>1~4——[苏]格拉宁

    一 只有在不实事求是的时候,事实才会叫人感兴趣. 虚构的人物任人摆布,并且纤毫毕露--他的一切想法意图,他的过去和未来,作者都一清二楚. 我还有一个任务:向读者灌输一些有用的知识,介绍些材料. 是一个 ...

  5. MVC+EF 多条件查询

    根据以前的做法是拼接sql语句,这会增加维护成本,因为sql语句里的内容不会报错,所以在使用ef的时候必须要抛弃拼接sql语句的习惯. 构建实例 List<vyw_user> list = ...

  6. spring 框架学习网站

    spring 框架学习网站 NO1 http://www.mkyong.com NO2 https://spring.io/docs/reference

  7. 本周对于java中lamdba表达式与内部进行了学习 ,以下是我在学习就中遇到的问题

    在java中,可以将一个类定义在另一个类里面或者一个方法里面,这样的类称为内部类.我觉得其实就是类的嵌套,在一个类中再定义一个类,这里已成员内部类为讲,内部类可以自由地运用外部类定义的方法,但外部类想 ...

  8. 一文读懂高性能网络编程中的I/O模型

    1.前言 随着互联网的发展,面对海量用户高并发业务,传统的阻塞式的服务端架构模式已经无能为力.本文(和下篇<高性能网络编程(六):一文读懂高性能网络编程中的线程模型>)旨在为大家提供有用的 ...

  9. mysql命令行创建表,插入表数据

    create table t_hero( id int unsigned auto_increment primary key, name varchar(10) unique not null, a ...

  10. 10分钟看懂Docker和K8S

    本文来源:鲜枣课堂 2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫"dotCloud"的公司. 这家公司主要提供基于PaaS的云计算技术服务.具体来说,是和LXC有关的 ...