Java有动态代理本质就是借助Java的字节码操作工具来实现在Java程序运行的时候动态生成相关的新的字节码,即新的类,并用新生成的类的对象实现相关额外操作动态代理在spring框架中被广泛地使用,主要有两种动态代理,一种是基于JDK本身的,别一种是基于cglib的,在spring中,如果一个类实现了某个接口,就用Jdk动态代理来实现,如果没有实现任何相关的接口,就用cglib来实现,下面就这两种实现动态代理的方法进行说明:
基于的jdk的动态代理,在被代理的类一定要实现一个接口:
假设某个类实现了BusinessInterface这个接口,接口中只有一个方法,定义如下:
package com.tds.jdk; public interface BusinessInterface { public void doSomething(); }接口的实现类定义如下:
package com.tds.jdk; public class BusinessObject implements BusinessInterface { @Override public void doSomething() { // TODO Auto-generated method stub System.out.println("方法正在执行..."); } }然后我们再写一个类LogHandler,这个类一定要实现InvocationHandler这个接口,InvocationHandler这个接口是jdk反射包中一个接口,这个接口中只有一个方法,接口定义如下:
package java.lang.reflect; /** * {@code InvocationHandler} is the interface implemented by * the <i>invocation handler</i> of a proxy instance. * * <p>Each proxy instance has an associated invocation handler. * When a method is invoked on a proxy instance, the method * invocation is encoded and dispatched to the {@code invoke} * method of its invocation handler. * * @author Peter Jones * @see Proxy * @since 1.3 */ public interface InvocationHandler { /** * Processes a method invocation on a proxy instance and returns * the result. This method will be invoked on an invocation handler * when a method is invoked on a proxy instance that it is * associated with. * * @param proxy the proxy instance that the method was invoked on * * @param method the {@code Method} instance corresponding to * the interface method invoked on the proxy instance. The declaring * class of the {@code Method} object will be the interface that * the method was declared in, which may be a superinterface of the * proxy interface that the proxy class inherits the method through. * * @param args an array of objects containing the values of the * arguments passed in the method invocation on the proxy instance, * or {@code null} if interface method takes no arguments. * Arguments of primitive types are wrapped in instances of the * appropriate primitive wrapper class, such as * {@code java.lang.Integer} or {@code java.lang.Boolean}. * * @return the value to return from the method invocation on the * proxy instance. If the declared return type of the interface * method is a primitive type, then the value returned by * this method must be an instance of the corresponding primitive * wrapper class; otherwise, it must be a type assignable to the * declared return type. If the value returned by this method is * {@code null} and the interface method's return type is * primitive, then a {@code NullPointerException} will be * thrown by the method invocation on the proxy instance. If the * value returned by this method is otherwise not compatible with * the interface method's declared return type as described above, * a {@code ClassCastException} will be thrown by the method * invocation on the proxy instance. * * @throws Throwable the exception to throw from the method * invocation on the proxy instance. The exception's type must be * assignable either to any of the exception types declared in the * {@code throws} clause of the interface method or to the * unchecked exception types {@code java.lang.RuntimeException} * or {@code java.lang.Error}. If a checked exception is * thrown by this method that is not assignable to any of the * exception types declared in the {@code throws} clause of * the interface method, then an * {@link UndeclaredThrowableException} containing the * exception that was thrown by this method will be thrown by the * method invocation on the proxy instance. * * @see UndeclaredThrowableException */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable; }
我们重点关注invoke这个方法,这个方法有三个参数,第一个参数是指要被代理的对象,第二个参数是要指要被代理实例的方法,第三个参数是被代理方法的实参
LogHandler类的定义如下:package com.tds.jdk; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class LogHandler implements InvocationHandler { private Object delegate; public LogHandler(Object delegate) { this.delegate = delegate; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object o = null; System.out.println("方法开始..." + method.getName()); o = method.invoke(this.delegate, args); System.out.println("方法结束..." + method.getName()); return o; } }测试类代码如下:
package com.tds.jdk; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class TestProxy { public static void main(String[] args) { BusinessInterface businessObject = new BusinessObject(); InvocationHandler logHandler = new LogHandler(businessObject); BusinessInterface proxy = (BusinessInterface)Proxy.newProxyInstance( businessObject.getClass().getClassLoader(), businessObject.getClass().getInterfaces(), logHandler); proxy.doSomething(); // System.out.println(businessObject.getClass().getClassLoader().getClass().getName()); // System.out.println(businessObject.getClass().getInterfaces()[0].getCanonicalName()); System.out.println(proxy.getClass().getName()); System.out.println(proxy instanceof Proxy); // System.out.println(BusinessObject.class.getName()); } }运行结果如下:
方法开始...doSomething
从输出的接口,我们可以看出,代理对象输出的结果确实是我们所预期的,代理的对象的类型是com.sun.proxy.$Proxy0,实际中所有通过Jdk实现的动态代理类的类型都是com.sun.proxy.$Proxy0, 并且这个类是java.lang.reflect.Proxy类的子类,我们跟踪一下整个运行过程:
首先进入到Proxy类的 newProxyInstance这个方法,方法的源码如下:
@CallerSensitive public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException { if (h == null) { throw new NullPointerException(); } final SecurityManager sm = System.getSecurityManager(); if (sm != null) { checkProxyAccess(Reflection.getCallerClass(), loader, interfaces); } /* * Look up or generate the designated proxy class. */ Class<?> cl = getProxyClass0(loader, interfaces); /* * Invoke its constructor with the designated invocation handler. */ try { final Constructor<?> cons = cl.getConstructor(constructorParams); final InvocationHandler ih = h; if (sm != null && ProxyAccessHelper.needsNewInstanceCheck(cl)) { // create proxy instance with doPrivilege as the proxy class may // implement non-public interfaces that requires a special permission return AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return newInstance(cons, ih); } }); } else { return newInstance(cons, ih); } } catch (NoSuchMethodException e) { throw new InternalError(e.toString()); } }这个方法的主要流程如下:
1.首先判断传进来的InvocationHandle接口实例对象是否为空,如果为空,则抛出异常结束;
2.然后判断系统的安全,这个是基于JVM,大多数情况都为空,如果不为空,还要进行访问权限检查;
3.根据 loader和interfaces参数,形成新的字节码类c1,在这里用到Java字节码操作框架;
4.根据得到的类c1和参数constructorParams,获得这个新类的构造器cons,其中constructorParams在Proxy中的定义为:
private static final Class<?>[] constructorParams = { InvocationHandler.class };
5.根据得到的构造器cons和传进来的参数h,由反射获得要被代码的对象;
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方法,proxyClassCache的定义如下:
private static final WeakCache<ClassLoader, Class<?>[], Class<?>> proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());从定义上可以看出,这个是一个WeakCache类的一个实例,WeakCache这个类的get方法定义如下:
public V get(K key, P parameter) { Objects.requireNonNull(parameter); expungeStaleEntries(); Object cacheKey = CacheKey.valueOf(key, refQueue); // lazily install the 2nd level valuesMap for the particular cacheKey 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; } } // create subKey and retrieve the possible Supplier<V> stored by that // subKey from valuesMap Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter)); Supplier<V> supplier = valuesMap.get(subKey); Factory factory = null; while (true) { if (supplier != null) { // supplier might be a Factory or a CacheValue<V> instance V value = supplier.get(); if (value != null) { return value; } } // else no supplier in cache // or a supplier that returned null (could be a cleared CacheValue // or a Factory that wasn't successful in installing the CacheValue) // lazily construct a Factory 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 retry with winning supplier } else { if (valuesMap.replace(subKey, supplier, factory)) { // successfully replaced // cleared CacheEntry / unsuccessful Factory // with our Factory supplier = factory; } else { // retry with current supplier supplier = valuesMap.get(subKey); } } } }这段代码主要的意思就是如果缓存中没有相关的代理类的字段码,就生成一份,并添加缓存中,并从缓存中取出;如果缓存中原来就有代理类的字节码,就直接从缓存中取出即可,根据java内存管理可知,管理类字节码的部分是存放在永久代的。