Java代理实现方法前后切入时间记录

IRecordAspect:

/**
 * 切入方法具体内容自己实现
 */
public interface IRecordAspect {
    long begin(Method method);

    void after(Method method,long begin);
}

IFunction:

/**
 * 要被代理的方法接口
 */
public interface IFunction {
    /**简单方法*/
	void execute();
    /**带传入返回参数方法*/
	int addition(int a, int b);
}

FunctionImpl:

/**
 * 被代理执行的具体方法
 */
public class FunctionImpl implements IFunction {

	@Override
	public void execute() {
		try {
			Thread.sleep(200);
			System.out.println("执行方法");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public int addition(int a, int b) {
		return a + b;
	}

}

FunctionProxyFactory:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class FunctionProxyFactory implements InvocationHandler {
	private IFunction delegate;
	private IRecordAspect aspect;

	public static FunctionProxyFactory of(IRecordAspect aspect) {
		return new FunctionProxyFactory(aspect);
	}

	private FunctionProxyFactory(IRecordAspect aspect) {
		this.aspect = aspect;
	}

	/**
	 * 绑定委托对象并返回一个代理类
	 * 
	 * @param delegate
	 * @return
	 */
	public Object aspect(IFunction delegate) {
		this.delegate = delegate;
		/**
		 * vl:类加载器<br>
		 * v2:代理类需要实现的接口<br>
		 * v3:InvocationHandler处理类<br>
		 */
		return Proxy.newProxyInstance(delegate.getClass().getClassLoader(), delegate.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		/** 切入具体方法前 */
		long begin = aspect.begin(method);
		/**
		 * 执行代理方法 <br>
		 * invoke:执行方法后的返回值,当方法定义为void时为null<br>
		 * v1:接口的实现类<br>
		 * v2:定义方法的传入参数<br>
		 */
		Object invoke = method.invoke(delegate, args);
		/** 切入具体方法后 */
		aspect.after(method, begin);
		return invoke;
	}

}

Main:

public class Main implements IRecordAspect {

	@Override
	public long begin(Method method) {
		long currentTimeMillis = System.currentTimeMillis();
		System.out.println("开始执行 :" + method.getName() + "->" + currentTimeMillis + "毫秒");
		return currentTimeMillis;
	}

	@Override
	public void after(Method method, long begin) {
		long currentTimeMillis = System.currentTimeMillis();
		System.out.println("执行" + method.getName() + "结束" + "->" + (currentTimeMillis) + "毫秒");
		System.out.println("用时 : " + (currentTimeMillis - begin) + "毫秒");
	}

	@Test
	public void test() {
		IFunction iRecord = (IFunction) FunctionProxyFactory.of(this).aspect(new FunctionImpl());
		iRecord.execute();
		System.out.println(iRecord.addition(3, 2));
	}
}

执行结果:

开始执行 :execute->1508726615900毫秒
执行方法
执行execute结束->1508726616103毫秒
用时 : 203毫秒
开始执行 :addition->1508726616103毫秒
执行addition结束->1508726616103毫秒
用时 : 0毫秒
5

 

转载于:https://my.oschina.net/spoon2014/blog/1554759

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值