动态代理

来源:互联网 发布:led控制软件下载 编辑:程序博客网 时间:2024/06/05 09:03

1.重要参数:

在java的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class),这一个类和接口是实现我们动态代理所必须用到的。
(1)InvocationHandler
每一个动态代理类都必须要实现InvocationHandler这个接口,并且每个代理类的实例都关联到了一个handler,当我们通过代理对象调用一个方法的时候,这个方法的调用就会被转发为由InvocationHandler这个接口的 invoke 方法来进行调用。我们来看看InvocationHandler这个接口的唯一一个方法 invoke 方法:

Object invoke(Object proxy, Method method, Object[] args) throws Throwableproxy:      指代我们所代理的那个真实对象method:     指代的是我们所要调用真实对象的某个方法的Method对象args:   指代的是调用真实对象某个方法时接受的参数

(2)Proxy
Proxy这个类的作用就是用来动态创建一个代理对象的类,它提供了许多的方法,但是我们用的最多的就是 newProxyInstance 这个方法:

public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentExceptionloader:         一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载interfaces:     一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口h:              一个InvocationHandler对象,表示的是会关联到哪一个InvocationHandler对象上

说白了就是用loader来加载代理对象的interfaces并且绑定到h上供代理对象调用

2.示例代码:

public interface Subject {    public void rent();    public void hello(String str);}

定义了一个类来实现这个接口,这个类就是我们的真实对象

public class RealSubject implements Subject {    @Override    public void rent() {        System.out.println("I want to rent my house");    }    @Override    public void hello(String str) {        System.out.println("hello: " + str);    }}

定义一个动态代理类了,前面说个,每一个动态代理类都必须要实现 InvocationHandler 这个接口

public class DynamicProxy implements InvocationHandler {    // 这个就是我们要代理的真实对象    private Object subject;    // 构造方法,给我们要代理的真实对象赋初值    public DynamicProxy(Object subject) {        this.subject = subject;    }    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Exception {        //在代理真实对象前我们可以添加一些自己的操作        //System.out.println("before rent house");        //当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用        method.invoke(subject, args);        //在代理真实对象后我们也可以添加一些自己的操作        //System.out.println("after rent house");        return null;    }}

测试:

    public static void main(String[] args) {        // 我们要代理的真实对象        Subject realSubject = new RealSubject();        // 我们要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的        InvocationHandler handler = new DynamicProxy(realSubject);        /*         * 通过Proxy的newProxyInstance方法来创建我们的代理对象,我们来看看其三个参数 第一个参数         * handler.getClass().getClassLoader()         * ,我们这里使用handler这个类的ClassLoader对象来加载我们的代理对象         * 第二个参数realSubject.getClass().getInterfaces(),我们这里为代理对象提供的接口是真实对象所实行的接口         * ,表示我要代理的是该真实对象,这样我就能调用这组接口中的方法了 第三个参数handler, 我们这里将这个代理对象关联到了上方的         * InvocationHandler 这个对象上         */        Subject subject = (Subject) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(),                realSubject.getClass().getInterfaces(), handler);        // 替代静态代理的new        subject.rent();        subject.hello("world");    }

输出:
I want to rent my house
hello: world

3.源码跟踪:

public class Proxy implements java.io.Serializable {    /** parameter types of a proxy class constructor */    private static final Class<?>[] constructorParams =        { InvocationHandler.class };    /**     * a cache of proxy classes     */    private static final WeakCache<ClassLoader, Class<?>[], Class<?>>        proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());    public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)        throws IllegalArgumentException    {        final Class<?>[] intfs = interfaces.clone();        /*         * Look up or generate the designated proxy class.         */        Class<?> cl = getProxyClass0(loader, intfs);        //******************返回proxyClassCache.get(loader, interfaces);有必要研究一下这个proxyClassCache如何初始化的        /*         * Invoke its constructor with the designated invocation handler.         */        try {            final Constructor<?> cons = cl.getConstructor(constructorParams);//获得动态代理的构造函数            final InvocationHandler ih = h;            return cons.newInstance(new Object[]{h});//生成实例        } catch (Exception e) {        }     }}

搜遍整个代码会发现proxyClassCache好像并没有初始化。但是getProxyClass0里面有段提示
// 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
也就是会通过KeyFactory生成一个key(这个key就是interface的hashCode),ProxyClassFactory生成一个代理类
ProxyClassFactory继承自BiFunction接口,重点关注apply函数

    private static final class ProxyClassFactory implements BiFunction<ClassLoader, Class<?>[], 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<Class<?>, 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) {                }                ......            }            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");                    }                }            }            /*             * 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) {            }        }    }

接下来看看generateProxyClass

    public static byte[] generateProxyClass(final String name,                                             Class<?>[] interfaces,                                             int accessFlags)     {         ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);         final byte[] classFile = gen.generateClassFile();//-----------------------------------         if (saveGeneratedFiles) {             java.security.AccessController.doPrivileged(             new java.security.PrivilegedAction<Void>() {                 public Void More ...run() {                     try {                         int i = name.lastIndexOf('.');                         Path path;                         if (i > 0) {                             Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));                             Files.createDirectories(dir);                             path = dir.resolve(name.substring(i+1, name.length()) + ".class");                         } else {                             path = Paths.get(name + ".class");                         }                         Files.write(path, classFile);                         return null;                     } catch (IOException e) {                         throw new InternalError(                             "I/O exception saving generated file: " + e);                     }                 }             });         }         return classFile;     }
     private byte[] generateClassFile() {         /* ============================================================          * Step 1: Assemble ProxyMethod objects for all methods to          * generate proxy dispatching code for.          */         addProxyMethod(hashCodeMethod, Object.class);         addProxyMethod(equalsMethod, Object.class);         addProxyMethod(toStringMethod, Object.class);         for (Class<?> intf : interfaces) {             for (Method m : intf.getMethods()) {                 addProxyMethod(m, intf);//------------------------------这里会调用invoke的接口方法             }         }         ......         return bout.toByteArray();     }
原创粉丝点击