`

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参见网址--http://songjianyong.iteye.com/blog/1638417
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics