public interface Greeting { public void sayHello(String name); }
public class GreetingImpl implements Greeting { @Override public void sayHello(String name) { System.out.println("Hello:"+name); } }
public class MyHandler implements InvocationHandler { Object instance; public Handler(Object instance) { this.instance = instance; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("====begin====="); //Object result = method.invoke(Proxy.newProxyInstance(instance.getClass().getClassLoader(), instance.getClass().getInterfaces(), this), args); Object result = method.invoke(instance, args); System.out.println("====end====="); return result; } }
public class Main { public static void main(String[] args) { Greeting instance = new GreetingImpl(); Handler h = new Handler(instance); Greeting g = (Greeting)Proxy.newProxyInstance(GreetingImpl.class.getClassLoader(), new Class[]{Greeting.class}, h ); g.sayHello("sssds"); } }
浙公网安备 33010602011771号