spring AOP配置

耗时监控
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);//通过反射获得拦截的method
if (m.isBridge()) {//如果是桥,则要获得实际拦截的method
for (int i = 0; i < args.length; i++) {
Class genClazz = GenericsUtils.getSuperClassGenricType(target.getClass());
if(args[i].getClass().isAssignableFrom(genClazz)){
parameterTypes[i] = 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参见网址--[url]http://songjianyong.iteye.com/blog/1638417[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值