package proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class MyInvocation implements InvocationHandler{
Object obj;
public void setObj(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
parameter.getName();
}
ProxyUtii.proxyMethod1(method.getName());
Object invoke = method.invoke(obj, args);
ProxyUtii.proxyMethod2(method.getName());
return invoke;
}
}
package proxy;
import java.lang.reflect.Proxy;
public class MyProxy {
public static Object getProxyInstance(Object obj) {
MyInvocation mi = new MyInvocation();
mi.setObj(obj);
Object newProxyInstance = Proxy.newProxyInstance(obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),mi);
return newProxyInstance;
}
}
package proxy;
public class ProxyUtii {
public static void proxyMethod1(String str) {
System.out.println("开始执行sql方法:"+str+"****************************************");
}
public static void proxyMethod2(String str) {
System.out.println("结束执行sql方法:"+str+"****************************************");
}
}
public static void main(String[] args) {
Object proxyInstance = MyProxy.getProxyInstance(pmaunewMapperImpl);
PmaunewMapper pm = (PmaunewMapper) proxyInstance;
List<Pmaunew> selectTest = pm.selectTest();
}