springAOP实现方法运行时间统计
aop的before和after,寻思分别在两个方法里获得当前时间,最后一减就可以了。
因此,今天就探讨了一下这个问题,和大家分享一下。
创建maven工程,引入spring的依赖包,配置好applicationContext-aop.xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
">
<!-- 切面所在的类包路径 -->
<context:component-scan base-package="com.product" />
<!-- 启动该切面代理 -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
</beans>
然后,创建两个包,一个来放切面类,一个放测试类。
测试类如下
package com.product.service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class TimeTest {
public void testMethod(){
System.out.println("执行方法");
}
public void testMethod2(){
int sum = 0;
for(int i=0;i<1000;i++){
sum += i;
}
System.out.println(sum);
System.out.println("执行方法2");
}
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext-aop.xml");
TimeTest A = (TimeTest)ctx.getBean("timeTest");
A.testMethod();
//A.testMethod2();
ctx.destroy();
}
}
写这个切面类。
package com.product.test.aopTest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MethodTimeMonitor {
private long startTime;
//声明切面类路径,类型必须为final String类型的,注解里要使用的变量只能是静态常量类型的
//public static final String POINT = "execution(* com.product.service.*.*(..))";
//也可以使用注解声明切入点,如下
@Pointcut("execution(* com.product.service.*.*(..))")
public void point(){}
@Before("point()")
public void doBefore(){
this.startTime = System.currentTimeMillis();
}
@After("point()")
public void doAfter(){
long endTime = System.currentTimeMillis();
System.out.println("方法执行了"+(endTime-this.startTime)+"ms");
}
}
现在,运行测试类,ok,看到如下结果:
说明次思路可行。
只是,当面试官继续问我before和after是写在两个方法中,那变量怎么能互通的时候,我竟然忘了它们本来就在一个类里,只需要把起始时间设为成员变量就好了,支吾了半天没答上来。
之后,笔者看了些资料,发现还有其他思路,比如aroud。
此思路和之前的没太大改变,只是在MethodTimeMonitor里,注掉之前的方法,新增around方法,如下:
@Around("point()")
public Object doAround(ProceedingJoinPoint pjp){
long startTime = System.currentTimeMillis();
Object obj = null;
try {
obj = pjp.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) pjp.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
System.out.println(methodName+"方法执行了"+(endTime-startTime)+"ms");
return obj;
}
好了,再次运行测试类,发现还是能得到一样的结果。
此外,笔者还了解到,aspect可以通过配置的方式实现,详见http://calatustela.iteye.com/blog/1910025。
另外,要提醒大家的是,注意:
1.配置文件尽可能放在src下,引入时直接加classpath就可以了。
2.<context:annotation-config />的作用是为使用到的注解自动配置对应的AnnotationBeanPostProcessor。而当使用自动扫描<context:component-scan
base-package="com.product.service" />后,它兼具有上述功能,因此两者取一即可。
此外,注意使用了spring注解的类包都要包括在内,否则可能某些包没有扫描,出现意外结果。
3.spring的注解bean加载机制中,获取某个bean用context.getBean("className");不过注意,className是该类首字母小写后的类名。若要自定义,可在注解后声明,如
@Controller("Name")。详见http://blog.csdn.net/damon834274634/article/details/38725909。
---------------------
转自:https://blog.csdn.net/baidu_41722481/article/details/79379040
springAOP实现方法运行时间统计的更多相关文章
- Python实现__metaclass__实现方法运行时间统计
几天前写的,参考了园友的一篇文章,链接找不到了.先感谢,找到了链接再补上.
- java监测方法运行时间/效率方法
前言: 这周在写一个小项目,虽然小但是是纯调外部接口的,调完了接口还不停的循环接口返回的数据(已转换JSONArray),然后再判断值,再做不同处理,关键是数据量还比较大,这刚做完还没开始上线,测试也 ...
- spring3.0框架检测方法运行时间测试(转)
主要利用了Spring AOP 技术,对想要统计的方法进行横切处理,方法执行前开始计时,方法执行后停止计时,得到计时方法就是该方法本次消耗时间. 步骤: 首先编写自己的Interceptor类来实现M ...
- 字典get方法和setdesault方法,统计message中各元素的出现频次
message= 'There are moments in life when you miss someone so much that you just want to pick them fr ...
- 统计方法运行时间【Java实现】
接口Command:定义命令的执行操作 package common; public interface Command { // 运行方法 void run(); } CommandRuntime ...
- java线程一之创建线程、线程池以及多线程运行时间统计
线程和进程的基本概念 进程和线程是动态的概念. 进程是 "执行中的程序",是一个动词,而程序是一个名词,进程运行中程序的"代码",而且还有自己的 ...
- 使用SpringAop 验证方法参数是否合法
(原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7683194) 1.依赖包 aspectjweaver.jar 其中Maven的配 ...
- [Done]Spring @Pointcut 切点调用不到(SpringAOP嵌套方法不起作用) 注意事项
今天在开发过程中,遇到一个问题卡了很久,测试代码如下: package spring.pointcut; import org.aspectj.lang.ProceedingJoinPoint; im ...
- 通过日志过滤的方法,统计每天内容详情页面的PV数
1.目的: 每天凌晨0点1分统计用户点击进入内容详情页的次数,对内容点击量形成榜单. 2.分析: A./data/log/epg.access.log日志实时打印用户访问页面的日志,并且每天凌晨0点会 ...
随机推荐
- CentOS安装配置Tomcat-7
安装环境:CentOS-6.5安装方式:源码安装软件:apache-tomcat-7.0.29.tar.gz下载地址:http://tomcat.apache.org/download-70.cgi ...
- JAVA 上加密算法的实现用例,MessageDigest介绍
第 1 章基础知识 1.1. 单钥密码体制 单钥密码体制是一种传统的加密算法,是指信息的发送方和接收方共同使用同一把密钥进行加解密. 通常 , 使用的加密算法 比较简便高效 , 密钥简短,加解密速度快 ...
- configparser模块读写ini配置文件
在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...
- Unity热更新技术整理
一.热更新学习介绍 1.什么是热更新 举例来说: 游戏上线后,玩家下载第一个版本(70M左右或者更大),在运营的过程中,如果需要更换UI显示,或者修改游戏的逻辑,这个时候,如果不使用热更新,就需要重新 ...
- el判断字符串是否为空
${empty 值} 返回true ,表示为空字符串; 在EL中empty对""和null的处理都返回true,而==null对""返回false,对null ...
- [LeetCode 题解]: Two Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- silverlight PopupWindow Resizeable兼容问题
下方第一段代码,在ie11中Resizeable无法生效,而在chrome中运行正常. HtmlPopupWindowOptions options = new HtmlPopupWindowOpti ...
- uwsgi启动提示:probably another instance of uWSGI is running on the same address (:8002). bind(): Address already in use [core/socket.c line 769]
提示8002端口被占用,可以这样终止掉后再启动 sudo fuser -k 8002/tcp
- 读《MacTalk·人生元编程》
读MackTalk人生元编程 花了几个晚上的时间把 卖桃君 的<MackTalk▪人生元编程>看完后意犹未尽,关掉kindle后回一下整本书的内容不由得笑了,因为脑海里出现了各种360°回 ...
- ASP.NET MVC中的控制器激活与反射之间的联系(帮助理解)
ASP.NET Mvc是ASP.NET的一个框架,同样也是基于管道的设计结构.HttpModule和HttpHandler是ASP.NET的两个重要组件,同样的在Mvc中也是非常重要的组件.在应用程序 ...