白白白

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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");
    }
}

 

posted on 2017-05-05 16:25  道至简  阅读(114)  评论(0)    收藏  举报