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

2017-10-23  本文已影响7人  DevSpoon

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
上一篇 下一篇

猜你喜欢

热点阅读