理解java代理模式

来源:互联网 发布:asp商城网站系统源码 编辑:程序博客网 时间:2024/05/17 21:27

1.java反射机制
理解代理模式的前提是先理解java中的反射机制,先看一个例子:

JDBC加载驱动时,Class.forName(“com.mysql.jdbc.Driver”);
此时通过反射加载连接mysql数据库的jar包,该句等价于import com.mysql.jdbc.Driver;
可是为什么不直接导入呢?这就是反射设计的合理之处了。
<1>,用反射可以在运行时动态导入,直接导入是在编译时就确定com.mysql.jdbc.Driver包必须存在,否则编译不过,这样看来,加上反射,可执行的范围增大了。
<2>,提高复用率,加上反射,Class.forName(“从配置文件读取具体的包内容”),这样,当你更换数据库时,只需更改配置文件,而不用像导入的方式那样挨个更换你的import。
java反射就是在运行时动态获取类的信息,方法,构造方法等信息。可以加载一个在运行时才确定其名称信息的类,并确定该类的基本信息。

2.由反射引出的设计模式-代理模式
代理模式概念理解:我自己能做的事情,让别人代替我来做,例如,我点了份菜,可以自己下楼去拿,但我现在比较忙,就让外卖小哥帮忙送上来,这块,外卖小哥就充当一个中间人的角色,帮我把事情做了。

3.静态代理模式
代理类由程序员自己实现的。就是再定义一个实现被代理类所实现的接口的代理类。
具体:

public interface People {    public void execute();}
public class My implements People {    @Override    public void execute() {         System.out.println("拿外卖");    }}
public class WaiMaiXiaoGe implements People {    public My my;    public WaiMaiXiaoGe(My my){        this.my=my;    }    @Override    public void execute() {        System.out.println("打包外卖");        my.execute();        System.out.println("送外卖结束");    }}

4.动态代理模式
概念:在运行的过程中运用反射动态创建代理类。
<1>JDK动态代理
01具体过程:
1.定义一个事件管理器类实现invocationHandle接口,并重写invoke(代理类,被代理的方法,方法的参数列表)方法。
2.实现被代理类及其实现的接口,
3.调用Proxy.newProxyInstance(类加载器,类实现的接口,事务处理器对象);生成一个代理实例。
4.通过该代理实例调用方法。
02代码实现:

public interface People {    public void execute();}
package com.Dream.Design;/** * @author wangpei * @version 创建时间:2017年4月22日 下午4:20:23 类说明 */public class My implements People {    @Override    public void execute() {        System.out.println("拿外卖");    }}
package com.Dream.Design;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/** * @author wangpei * @version 创建时间:2017年4月22日 下午5:31:23 类说明 */public class Handler implements InvocationHandler {    private Object o = null;    public Handler(Object o) {        this.o = o;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args)            throws Throwable {        System.out.println("外卖小哥取外卖");        method.invoke(o, args);        System.out.println("送外卖完成");        return null;    }}
package com.Dream.Design;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;/** * @author wangpei * @version 创建时间:2017年4月22日 下午5:31:23 类说明 */public class Handler implements InvocationHandler {    private Object o = null;    public Handler(Object o) {        this.o = o;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args)            throws Throwable {        System.out.println("外卖小哥取外卖");        method.invoke(o, args);        System.out.println("送外卖完成");        return null;    }}

结果:

外卖小哥取外卖拿外卖送外卖完成

03具体分析
怎么通过Proxy.newProxyInstance(,,,,)就能生成一个代理实例呢。
我们分析方法内部:

 public static Object newProxyInstance(ClassLoader loader,     Class<?>[] interfaces,InvocationHandler h) throws       IllegalArgumentException    {        Objects.requireNonNull(h);//事务处理器为空抛出异常。        final Class<?>[] intfs = interfaces.clone();        //被代理类实现的接口数组        Class<?> cl = getProxyClass0(loader, intfs);//获得代理类         final Constructor<?> cons = cl.getConstructor(constructorParams);//获得代理类的构造方法            //返回构造方法的实例            return cons.newInstance(new Object[]{h});    }

ps:上面是我把验证部分的处理删了的源码,千万别以为源码长这样。。

当然,我们要看一看getProxyClass0()方法的具体实现

private static Class<?> getProxyClass0(ClassLoader loader,                                           Class<?>... interfaces) {        if (interfaces.length > 65535) {            throw new IllegalArgumentException("interface limit exceeded");        }        // If the proxy class defined by the given loader implementing        // the given interfaces exists, this will simply return the cached copy;        // otherwise, it will create the proxy class via the ProxyClassFactory        return proxyClassCache.get(loader, interfaces);        //###############    }

如上图#处,那再看proxyClassCache.get(loader, interfaces);的实现

public V get(K key, P parameter) {        Objects.requireNonNull(parameter);        expungeStaleEntries();        Object cacheKey = CacheKey.valueOf(key, refQueue);        ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey);        if (valuesMap == null) {            ConcurrentMap<Object, Supplier<V>> oldValuesMap                = map.putIfAbsent(cacheKey,                                  valuesMap = new ConcurrentHashMap<>());            if (oldValuesMap != null) {                valuesMap = oldValuesMap;            }        }        Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));//#######        Supplier<V> supplier = valuesMap.get(subKey);        Factory factory = null;        while (true) {            if (supplier != null) {                V value = supplier.get();                if (value != null) {                    return value;                }            }            if (factory == null) {                factory = new Factory(key, parameter, subKey, valuesMap);            }            if (supplier == null) {                supplier = valuesMap.putIfAbsent(subKey, factory);                if (supplier == null) {                    // successfully installed Factory                    supplier = factory;                }            } else {                if (valuesMap.replace(subKey, supplier,factory)){                  supplier = factory;                } else {                    supplier = valuesMap.get(subKey);                }            }        }    }

再继续往下,我们找到根源:ProxyClassFactory

private static final class ProxyClassFactory        implements BiFunction[], Class>>    {        // prefix for all proxy class names        private static final String proxyClassNamePrefix = "$Proxy";        // next number to use for generation of unique proxy class names        private static final AtomicLong nextUniqueNumber = new AtomicLong();        @Override        public Class> apply(ClassLoader loader, Class>[] interfaces) {            Map, Boolean> interfaceSet = new IdentityHashMap(interfaces.length);            for (Class> intf : interfaces) {                /*                 * Verify that the class loader resolves the name of this                 * interface to the same Class object.                 */                Class> interfaceClass = null;                try {                    interfaceClass = Class.forName(intf.getName(), false, loader);                } catch (ClassNotFoundException e) {                }                if (interfaceClass != intf) {                    throw new IllegalArgumentException(                        intf + " is not visible from class loader");                }                /*                 * Verify that the Class object actually represents an                 * interface.                 */                if (!interfaceClass.isInterface()) {                    throw new IllegalArgumentException(                        interfaceClass.getName() + " is not an interface");                }                /*                 * Verify that this interface is not a duplicate.                 */                if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {                    throw new IllegalArgumentException(                        "repeated interface: " + interfaceClass.getName());                }            }            String proxyPkg = null;     // package to define proxy class in            int accessFlags = Modifier.PUBLIC | Modifier.FINAL;            /*             * Record the package of a non-public proxy interface so that the             * proxy class will be defined in the same package.  Verify that             * all non-public proxy interfaces are in the same package.             */            for (Class> intf : interfaces) {                int flags = intf.getModifiers();                if (!Modifier.isPublic(flags)) {                    accessFlags = Modifier.FINAL;                    String name = intf.getName();                    int n = name.lastIndexOf('.');                    String pkg = ((n == -1) ? "" : name.substring(0, n + 1));                    if (proxyPkg == null) {                        proxyPkg = pkg;                    } else if (!pkg.equals(proxyPkg)) {                        throw new IllegalArgumentException(                            "non-public interfaces from different packages");                    }                }            }            if (proxyPkg == null) {                // if no non-public proxy interfaces, use com.sun.proxy package                proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";            }            /*             * Choose a name for the proxy class to generate.             */            long num = nextUniqueNumber.getAndIncrement();            String proxyName = proxyPkg + proxyClassNamePrefix + num;            /*             * Generate the specified proxy class.             */              //######################            byte[] proxyClassFile = ProxyGenerator.generateProxyClass(                proxyName, interfaces, accessFlags);            try {                return defineClass0(loader, proxyName,                                    proxyClassFile, 0, proxyClassFile.length);            } catch (ClassFormatError e) {                /*                 * A ClassFormatError here means that (barring bugs in the                 * proxy class generation code) there was some other                 * invalid aspect of the arguments supplied to the proxy                 * class creation (such as virtual machine limitations                 * exceeded).                 */                throw new IllegalArgumentException(e.toString());            }        }    }

上面代码很容易看出,
调用ProxyGenerator.generateProxyClass生成代理类。
byte[] proxyClassFile字节码反编译后发现:
生成代理类 class ProxySubject extends Proxy implements Subject

现在我们总结一下,当我们调用代理类的方法时(即上面的步骤4),会通过反射处理为调用,实现管理器的实现类中的invote()方法,调用
handler.invoke(this, m3, null);

还有许多不太透彻的地方,望批评指正,下节介绍代理模式在spring aop中的具体应用。

0 0