六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 4|回复: 0

spring AOP 简单性能监视器

[复制链接]

升级  14%

176

主题

176

主题

176

主题

进士

Rank: 4

积分
570
 楼主| 发表于 2013-2-3 11:07:33 | 显示全部楼层 |阅读模式
package cn.com.tcgroup.yunlu.commons;import java.lang.reflect.Method;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.reflect.MethodSignature;public class EfficiencyAOP {private long time = 0;private String className = null;private String methodName = null;public void begin(JoinPoint point){time = System.currentTimeMillis();Object target = point.getTarget();//拦截的实体类className = target.getClass().getName();methodName = point.getSignature().getName();//拦截的方法名称Object[] args = point.getArgs();//拦截的方法参数Class[] parameterTypes = ((MethodSignature)point.getSignature()).getMethod().getParameterTypes();//拦截的方法参数类型Method  m = null;try {m = target.getClass().getMethod(methodName, parameterTypes);//通过反射获得拦截的methodif (m.isBridge()) {//如果是桥,则要获得实际拦截的methodfor (int i = 0; i < args.length; i++) {Class genClazz = GenericsUtils.getSuperClassGenricType(target.getClass());if(args.getClass().isAssignableFrom(genClazz)){parameterTypes = genClazz;}}m = target.getClass().getMethod(methodName, parameterTypes);}} catch (SecurityException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();}}public void end(){System.out.println(className+"."+methodName + "耗时" + (System.currentTimeMillis()-time) + "毫秒");}}


<bean id="efficiencyAOP" class="cn.com.tcgroup.yunlu.commons.EfficiencyAOP"></bean><!-- 配置性能监视器 --><aop:config><aop:aspect id="test" ref="efficiencyAOP"><aop:pointcut expression="execution (* cn.com.tcgroup.yunlu.*.*.*.*.*(..))" id="p"/><aop:before method="begin" pointcut-ref="p"/><aop:after method="end" pointcut-ref="p"/></aop:aspect></aop:config>


备注:GenericsUtils.java参见网址--http://songjianyong.iteye.com/blog/1638417
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表