java动态代理

@Test
    public void test(){
        
        //new UserImpl();
    /*    ProxyT  proxy=new ProxyT(new UserImpl());
        proxy.add();
        proxy.update();*/
        final User user=new UserImpl();
        User userProxy=(User) Proxy.newProxyInstance(user.getClass().getClassLoader(), new Class[]{User.class}, new InvocationHandler(){
            /**
             * 参数
             * ClassLoader loader:被代理的类加载器
             * Class<?>[] interfaces:被代理类的接口,注意这里的数组一定是接口类,否则出错
             * InvocationHandler h:将具体调用代理对象时产生的行为
             */
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                /**
                 * proxy:
                 * method:代理类正在调用的方法,该处是add
                 * args:add方法所传入的参数
                 */
                // TODO Auto-generated method stub
                System.out.println(method);
                System.out.println("hehehehehhehe");
                //method.invoke(user, args);
                return 0;
            }
            
        });
        userProxy.add();
    }

posted @ 2014-06-23 16:15  清风yi竹  阅读(96)  评论(0编辑  收藏  举报