public interface HelloWorld { public void sayHelloWorld();}
public class HelloWorldImpl implements HelloWorld { @Override public void sayHelloWorld() { System.out.println("Hello World"); } }public class JdkProxyExample implements InvocationHandler { private Object target; public Object bind(Object target){ this.target=target; return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("进入代理逻辑方法"); System.out.println("在调度真实对象之前的服务"); Object object=method.invoke(target,args); System.out.println("在调度真实对象之后的服务"); return object; } }public static void main(String args[]){ JdkProxyExample jdkProxyExample=new JdkProxyExample(); HelloWorld helloWorld= (HelloWorld) jdkProxyExample.bind(new HelloWorldImpl()); helloWorld.sayHelloWorld(); }