• 动态代理 dynamicProxy


    使用代理处理类,需要实现InvocationHandler接口

    package com.lan.dynamicProxy.myDynamicProxy;
    
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.util.Arrays;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 上午10:12
     * To change this template use File | Settings | File Templates.
     */
    public class MyHandler implements InvocationHandler {
    
        /**
         * 需要实现动态代理的目标对象
         */
        private final Object target;
    
        public MyHandler(Object target) {
            this.target = target;
        }
    
        /**
         * 只要实现了InvocationHandler,并且接收一个对象作为参数,就可以为该对象建立动态代理了
         */
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("事务处理前..."+"invoke --  "+method.getName()+" : "+ Arrays.deepToString(args));
            Object result = method.invoke(target, args);//To change body of implemented methods use File | Settings | File Templates.
            System.out.println("事务处理后");
            return result;
        }
    
         static void myproxy(){
    
         }
    }

    接口1

    package com.lan.dynamicProxy.myDynamicProxy;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 上午10:15
     * To change this template use File | Settings | File Templates.
     */
    public interface MyInterface {
        public void sing(String name);
        public void jump(String name);
        public void sleep(String name);
    }

    接口2

    package com.lan.dynamicProxy.myDynamicProxy;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 上午10:15
     * To change this template use File | Settings | File Templates.
     */
    public interface MyInterface1 {
        public void greet(String greet);
    }

    一般业务类,实现类上面的2个接口:MyInterface,MyInterface1

    package com.lan.dynamicProxy.myDynamicProxy;
    
    /**
     * 这就是需要实现动态代理的目标类
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 上午10:16
     */
    public class MyImpl implements MyInterface, MyInterface1 {
        @Override
        public void sing(String name) {
            System.out.println(name + "小鸟在歌唱");
        }
    
        @Override
        public void jump(String name) {
            System.out.println(name + "我不是跳蚤");
        }
    
        @Override
        public void sleep(String name) {
            System.out.println(name + "小熊在睡觉");
        }
    
        @Override
        public void greet(String greet) {
            System.out.println("Hello," + greet);
        }
    }

    测试类:

    package com.lan.dynamicProxy.myDynamicProxy;
    
    import com.lan.dynamicProxy.LoggingInvocationHandler;
    
    import java.lang.reflect.Proxy;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Set;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 上午10:18
     * To change this template use File | Settings | File Templates.
     */
    public class Test {
        static  void test0(){
            //首先建立一个目标对象
            MyImpl impl=new MyImpl();
            //将目标对象传给实现了接口InvocationHandler的处理业务实现类
            MyHandler myHandler=new MyHandler(impl);
    
            //创建一个动态代理对象,arg0:targetObject的一个classLoader实例,arg1:目标对象实现的接口,arg2:处理业务实现类
    //        MyInterface myInterface= (MyInterface) Proxy.newProxyInstance(MyImpl.class.getClassLoader(), new Class[]{MyInterface.class,MyInterface1.class}, myHandler);
            Object obj=Proxy.newProxyInstance(impl.getClass().getClassLoader(),impl.getClass().getInterfaces(),myHandler);
            MyInterface myInterface= (MyInterface) obj;
            //调用目标对象的接口的方法
            myInterface.jump("小明,");
            MyInterface1 myInterface1= (MyInterface1) myInterface;
            myInterface1.greet("Bom");
    
        }
        
        /**如果实现的接口出现同名方法,那么实现排在前面的方法*/
        public static void test1(){
            List list=new ArrayList();
            LoggingInvocationHandler handler=new LoggingInvocationHandler(list);
            Object proxy=Proxy.newProxyInstance(list.getClass().getClassLoader(),new Class[]{List.class,Set.class},handler);
            //如果换成这句,下面就会出错,因为handler接受到的是ArrayList对象,其并未实现Set接口,若Set.class排在前面,代理对象实现的是Set接口中的add方法
    //        Object proxy=Proxy.newProxyInstance(list.getClass().getClassLoader(),new Class[]{Set.class,List.class},handler); 
            List proxylist= (List) proxy;
            Set proxySet= (Set) proxy;
            proxylist.add("list add");
            proxySet.add("set add");
            proxylist.add(1);
            proxySet.add(1);
            System.out.println(Arrays.deepToString(proxylist.toArray()));
            System.out.println(Arrays.deepToString(proxySet.toArray()));
            System.out.println(list.hashCode()+" "+proxylist.hashCode()+" "+proxySet.hashCode());
        }
        public static void main(String[] args) {
            test1();
        }
    
    }

    使用代理工厂创建代理类:

    package com.lan.dynamicProxy.myDynamicProxy;
    
    import com.lan.dynamicProxy.LoggingInvocationHandler;
    
    import java.lang.reflect.Proxy;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Njoy
     * Date: 13-8-21
     * Time: 下午10:38
     * To change this template use File | Settings | File Templates.
     */
    public class ProxyFactory {
    
        public static Object SingleInterfaceProxy(Class clz, Object target) {
            LoggingInvocationHandler handler = new LoggingInvocationHandler(target);
            return Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[]{clz}, handler);
        }
    
        public static Object MultiInterfaceProxy(Class[] classes, Object target) {
            LoggingInvocationHandler handler = new LoggingInvocationHandler(target);
            return Proxy.newProxyInstance(target.getClass().getClassLoader(), classes, handler);
        }
    
        public static Object AllInterfaceProxy(Object target) {
            LoggingInvocationHandler handler = new LoggingInvocationHandler(target);
            return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
        }
    
    
    }
  • 相关阅读:
    [hdu4631 Sad Love Story]最近点对,枚举
    [hdu4629 Burning]三角形面积并,扫描线
    [hdu4628 Pieces]二进制子状态,DP
    [hdu4627 The Unsolvable Problem]数论
    [hdu4622 Reincarnation]后缀数组
    [hdu5379 Mahjong tree]dfs计数
    [hdu5375 Gray code]DP
    决策树
    算法题解之位运算
    算法题解之滑动窗口
  • 原文地址:https://www.cnblogs.com/lansor/p/3277369.html
Copyright © 2020-2023  润新知