简单Java动态代理实现

时间:2023-03-09 01:21:08
简单Java动态代理实现
package test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) { //Proxy.newProxyInstance
// 参数:1. 类加载器
// 2.要代理的接口 new Class[] {IHello.class}
// 3.实际处理接口方法的对象
// IHello.class
///
IHello iHello= (IHello)Proxy.newProxyInstance(Test.class.getClassLoader(), new Class[] {IHello.class} , new InvocationHandler() { @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("zhangxiongfeng");
System.out.println("----"+method.getName());
//proxy 生成的代理对象
// 执行的方法
//args 实际传入的参数
return proxy;
}
});
iHello.get();
} }
package test;

public interface IHello {

    public void get();

}