30 lines
731 B
Java
Executable File
30 lines
731 B
Java
Executable File
package com.thinker.common.proxy;
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
import java.lang.reflect.Method;
|
|
|
|
/**
|
|
* Created by lzh on 8/3/16.
|
|
* 利用反射调用实现类的方法,实现动态代理。
|
|
*
|
|
*/
|
|
public class Handler<T> implements InvocationHandler {
|
|
private Class<T> t;
|
|
public Handler(Class<T> t){
|
|
this.t = t;
|
|
}
|
|
private void before(){
|
|
System.out.println("before");
|
|
}
|
|
private void after(){
|
|
System.out.println("after");
|
|
}
|
|
@Override
|
|
public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
|
|
this.before();
|
|
Object object = method.invoke(t.newInstance(),args);
|
|
this.after();
|
|
return object;
|
|
}
|
|
}
|