JDK动态代理需要接口,如下:
public interface proxyInf {
public void print();
}
直接测试了:
public class MyProxy implements proxyInf{
public static void main(String[] args) {
proxyInf target = new MyProxy();
proxyInf newProxyInstance = (proxyInf)Proxy.newProxyInstance(target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("do pre");
Object obj = method.invoke(target, args);
System.out.println("do post");
//本例中,这里不用返回obj也行
return obj;
}
});
newProxyInstance.print();
}
public void print() {
System.out.println("sssss");
}
}