- public interface IHelloWorld {
- void sayHello();
- }
- public class HelloWorldImpl implements IHelloWorld {
- public void sayHello() {
- System.out.println("Hello World!!");
- }
- }
- public class HelloWorldHandler implements InvocationHandler {
- IHelloWorld object;
- public HelloWorldHandler(IHelloWorld object) {
- this.object = object;
- }
- public Object invoke(Object proxy, Method method, Object[] args)
- throws Throwable {
- System.out.println("before");
- Object o = method.invoke(object, args);
- System.out.println("after");
- return o;
- }
- }
- public class proxyDemo {
- public static void main(String[] args) {
- InvocationHandler handler = new HelloWorldHandler(new HelloWorldImpl());
- IHelloWorld proxy = (IHelloWorld)Proxy.newProxyInstance(
- HelloWorldImpl.class.getClassLoader(),
- HelloWorldImpl.class.getInterfaces(),
- handler);
- proxy.sayHello();
- }
- }
before
Hello World!! after