Java的三种代理模式&完整源码分析
Java的三种代理模式&完整源码分析参考资料: 博客园-Java的三种代理模式 简书-JDK动态代理-超详细源码分析 [博客园-WeakCache缓存的实现机制](https://www.cnblogs.com/liuyun1995/p/8144676.html) 静态代理静态代理在使用时,需要定义接口或者父类,被代理对象与代理对象一起实现相同的接口或者是继承相同父类
如何解决静态代理中的缺点呢?答案是可以使用动态代理方式 实现静态代理的步骤
定义接口 UserInterface public interface UserInterface { // 保存用户信息 void save(); } 定义接口的实现类 UserService public class UserService implements UserInterface { @Override public void save() { System.out.println("[静态代理] 保存用户信息"); } } 定义静态代理 UserProxy public class UserProxy implements UserInterface { private UserInterface userInterface; public UserProxy(UserInterface userInterface) { this.userInterface = userInterface; } /** * save 代理方法 */ @Override public void save() { // 调用目标方法前处理 System.out.println("[静态代理] save 开始代理..."); // 调用目标方法 userInterface.save(); // 调用目标方法后处理 System.out.println("[静态代理] save 结束代理..."); } } 测试客户端 public class Client { public static void main(String[] args) { // 新建目标对象 UserService userService = new UserService(); // 创建目标对象的代理对象 UserProxy userProxy = new UserProxy(userService); // 执行代理对象 userProxy.save(); } } 动态代理Java动态代理的优势是实现无侵入式的代码扩展,也就是方法的增强;让你可以在不用修改源码的情况下,增强一些方法;在方法的前后你可以做你任何想做的事情(甚至不去执行这个方法就可以) 特点
JDK动态代理
问题
Proxy.java->ProxyClassFactory->apply(); /* * Verify that the Class object actually represents an * interface. */ // interfaceClass 指的是 Proxy.newProxyInstance 中的 interfaces if (!interfaceClass.isInterface()) { throw new IllegalArgumentException( interfaceClass.getName() + " is not an interface"); } 注意该方法是在Proxy类中是静态方法,且接收的三个参数依次为:
实现JDK动态代理的步骤
定义接口 UserInterface public interface UserInterface { // 保存用户信息 void save(); // 更新用户信息 void update(); } 定义接口的实现类 UserService public class UserService implements UserInterface { @Override public void save() { System.out.println("[JDK动态代理] 保存用户信息"); } @Override public void update() { System.out.println("[JDK动态代理] 更新用户信息"); } } 定义代理工厂 ProxyFactory public class ProxyFactory { // 维护的目标对象 private Object target; private Class<?> clazz; public ProxyFactory(Object target,Class<?> clazz) { this.target = target; this.clazz = clazz; } // 获取代理对象 public Object getProxyObjectByClazz() { return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class[]{clazz},(proxy,method,args) -> { System.out.println("[JDK动态代理] save 开始代理..."); System.out.println("当前线程名称:" + Thread.currentThread().getName()); String className = method.getDeclaringClass().getName(); System.out.println("目标对象类名称:" + className); String methodName = method.getName(); System.out.println("目标对象方法名:" + methodName); Class<?>[] parameterTypes = method.getParameterTypes(); System.out.println("目标对象参数:" + parameterTypes); // 执行目标对象并获取返回值/该方法后面不会执行 // Object returnValue = method.invoke(target,args); System.out.println("[JDK动态代理] save 结束代理..."); return null; }); } /** * 获取代理对象 * * @return */ public Object getProxyObjectByTarget() { return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),args) -> { System.out.println("[JDK动态代理] save 开始代理..."); System.out.println("当前线程名称:" + Thread.currentThread().getName()); String className = method.getDeclaringClass().getName(); System.out.println("目标对象类名称:" + className); String methodName = method.getName(); System.out.println("目标对象方法名:" + methodName); Class<?>[] parameterTypes = method.getParameterTypes(); System.out.println("目标对象参数:" + parameterTypes); // 执行目标对象并获取返回值 Object returnValue = method.invoke(target,args); System.out.println("[JDK动态代理] save 结束代理..."); return returnValue; }); } } 测试客户端 public class Client { public static void main(String[] args) { System.out.println("********************* 使用接口生成代理对象 *********************"); System.out.println("当前线程:" + Thread.currentThread().getName()); UserInterface proxy = (UserInterface) new ProxyFactory(null,UserInterface.class).getProxyObjectByClazz(); System.out.println("代理对象类型:" + proxy.getClass()); proxy.save(); System.out.println("********************* 使用实现类生成代理对象 *********************"); UserService userService = new UserService(); System.out.println("目标对象类型:" + userService.getClass()); UserInterface proxy2 = (UserInterface) new ProxyFactory(userService,null).getProxyObjectByTarget(); System.out.println("代理对象类型:" + proxy2.getClass()); proxy2.update(); } } 输出结果 ********************* 使用接口生成代理对象 ********************* 当前线程:main 代理对象类型:class com.sun.proxy.$Proxy0 [JDK动态代理] save 开始代理... 当前线程名称:main 目标对象类名称:com.example.spring_boot.modules.study.proxyobject.jdkproxy.UserInterface 目标对象方法名:save 目标对象参数:[Ljava.lang.Class;@c038203 [JDK动态代理] save 结束代理... ********************* 使用实现类生成代理对象 ********************* 目标对象类型:class com.example.spring_boot.modules.study.proxyobject.jdkproxy.UserService 代理对象类型:class com.sun.proxy.$Proxy0 [JDK动态代理] save 开始代理... 当前线程名称:main 目标对象类名称:com.example.spring_boot.modules.study.proxyobject.jdkproxy.UserInterface 目标对象方法名:update 目标对象参数:[Ljava.lang.Class;@cb5822 [JDK动态代理] 更新用户信息 [JDK动态代理] save 结束代理... 运行结果和静态代理一样,说明成功了。但是,我们注意到,我们并没有像静态代理那样去自己定义一个代理类,并实例化代理对象。实际上,动态代理的代理对象是在内存中的,是JDK根据我们传入的参数生成好的。那动态代理的代理类和代理对象是怎么产生的呢?重头戏来了,且往下看 JDK动态代理源码分析代理对象的入口 Proxy.java->newProxyInstance(); // 1. 查找或生成指定的代理类(下面会详细说明该部分内容) Class<?> cl = getProxyClass0(loader,intfs); // 2. 根据Class获取构造器 final Constructor<?> cons = cl.getConstructor(constructorParams); // 3. 返回实例化的构造器 return cons.newInstance(new Object[]{h}); 详细说说 getProxyClass0 这个方法 private static Class<?> getProxyClass0(ClassLoader loader,Class<?>... interfaces) { // 限定代理的接口不能超过65535个 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 // 如果给定加载程序定义的代理类实现 // 给定的接口存在,这只会返回缓存的副本; // 否则,它将通过proxyclassfactory创建代理类 return proxyClassCache.get(loader,interfaces); } 说明一下上面提到的 proxyClassCache // proxyClassCache变量是在Proxy.java中的静态变量 // 一个静态的 proxy class 缓存对象 private static final WeakCache<ClassLoader,Class<?>[],Class<?>> proxyClassCache = new WeakCache<>(new KeyFactory(),new ProxyClassFactory()); /* 那就再探究一下 WeakCache 这个类 */ final class WeakCache<K,P,V> { // Reference引用队列 private final ReferenceQueue<K> refQueue = new ReferenceQueue<>(); // the key type is Object for supporting null key // 使用了二级缓存技术,key为一级缓存,value为二级缓存,key是Object类型是为了存储null private final ConcurrentMap<Object,ConcurrentMap<Object,Supplier<V>>> map = new ConcurrentHashMap<>(); // reverseMap记录了所有代理类生成器是否可用,这是为了实现缓存的过期机制 private final ConcurrentMap<Supplier<V>,Boolean> reverseMap = new ConcurrentHashMap<>(); // 生成二级缓存key的工厂,这里传入的是KeyFactory private final BiFunction<K,?> subKeyFactory; // 生成二级缓存value的工厂,这里传入的是ProxyClassFactory private final BiFunction<K,V> valueFactory; /** * Construct an instance of {@code WeakCache} * * @param subKeyFactory a function mapping a pair of * {@code (key,parameter) -> sub-key} * @param valueFactory a function mapping a pair of * {@code (key,parameter) -> value} * @throws NullPointerException if {@code subKeyFactory} or * {@code valueFactory} is null. */ // 构造器,上面初始化proxyClassCache用到的 public WeakCache(BiFunction<K,?> subKeyFactory,BiFunction<K,V> valueFactory) { this.subKeyFactory = Objects.requireNonNull(subKeyFactory); this.valueFactory = Objects.requireNonNull(valueFactory); } /** * Look-up the value through the cache. This always evaluates the * {@code subKeyFactory} function and optionally evaluates * {@code valueFactory} function if there is no entry in the cache for given * pair of (key,subKey) or the entry has already been cleared. * * @param key possibly null key * @param parameter parameter used together with key to create sub-key and * value (should not be null) * @return the cached value (never null) * @throws NullPointerException if {@code parameter} passed in or * {@code sub-key} calculated by * {@code subKeyFactory} or {@code value} * calculated by {@code valueFactory} is null. */ // 这个方法我们下面详细讲 public V get(K key,P parameter) { ... } ... } 上面的一个小插曲,现在继续讲 WeakCache.java 中的 get 方法 // K和P就是WeakCache定义中的泛型,key是类加载器,parameter是接口类数组 public V get(K key,P parameter) { // 验证接口类数组不为空 Objects.requireNonNull(parameter); // 清除无效的缓存 expungeStaleEntries(); // 将ClassLoader包装成CacheKey,作为一级缓存的key 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) { // CAS方式put,如果不存在则放入,存在则不放入。放入后会返回null,没有放入会返回当前的value ConcurrentMap<Object,Supplier<V>> oldValuesMap = map.putIfAbsent(cacheKey,valuesMap = new ConcurrentHashMap<>()); // 如果oldValuesMap有值,说明放入失败,也说明已经存在了,会把 valuesMap 刷新回以前存在的值 if (oldValuesMap != null) { valuesMap = oldValuesMap; } } // create subKey and retrieve the possible Supplier<V> stored by that // subKey from valuesMap // 根据代理类实现的接口数组来生成二级缓存key,分为key0,key1,key2,keyx Object subKey = Objects.requireNonNull(subKeyFactory.apply(key,parameter)); // 根据subKey获取到二级缓存的值 Supplier<V> supplier = valuesMap.get(subKey); Factory factory = null; // 这个循环提供了轮询机制,如果条件为假就继续重试直到条件为真为止 while (true) { if (supplier != null) { // supplier might be a Factory or a CacheValue<V> instance // 在这里supplier可能是一个Factory也可能会是一个CacheValue // 在这里不作判断,而是在Supplier实现类的get方法里面进行验证 // 下面详细讲这个方法 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实例作为subKey对应的值 factory = new Factory(key,parameter,subKey,valuesMap); } if (supplier == null) { // 到这里表明subKey没有对应的值,就将factory作为subKey的值放入 supplier = valuesMap.putIfAbsent(subKey,factory); if (supplier == null) { // successfully installed Factory // 到这里表明成功将factory放入缓存 supplier = factory; } // else retry with winning supplier } else { // 否则,可能期间有其他线程修改了值,那么就不再继续给subKey赋值,而是取出来直接用 if (valuesMap.replace(subKey,supplier,factory)) { // successfully replaced // cleared CacheEntry / unsuccessful Factory // with our Factory // 成功将factory替换成新的值 supplier = factory; } else { // retry with current supplier // 替换失败,继续使用原先的值 supplier = valuesMap.get(subKey); } } } } WeakCache的get方法并没有用锁进行同步,那它是怎样实现线程安全的呢?因为它的所有会进行修改的成员变量都使用了ConcurrentMap,这个类是线程安全的。因此它将自身的线程安全委托给了ConcurrentMap, get方法尽可能的将同步代码块缩小,这样可以有效提高WeakCache的性能。我们看到ClassLoader作为了一级缓存的key,这样可以首先根据ClassLoader筛选一遍,因为不同ClassLoader加载的类是不同的。然后它用接口数组来生成二级缓存的key,这里它进行了一些优化,因为大部分类都是实现了一个或两个接口,所以二级缓存key分为key0,key1,key2,keyX。key0到key2分别表示实现了0到2个接口,keyX表示实现了3个或以上的接口,事实上大部分都只会用到key1和key2。这些key的生成工厂是在Proxy类中,通过WeakCache的构造器将key工厂传入。这里的二级缓存的值是一个Factory实例,最终代理类的值是通过Factory这个工厂来获得的 再详细讲 supplier.get() @Override public synchronized V get() { // serialize access // re-check // 从二级缓存里面再获取Supplier,用来验证是否是Factory本身 Supplier<V> supplier = valuesMap.get(subKey); if (supplier != this) { // something changed while we were waiting: // might be that we were replaced by a CacheValue // or were removed because of failure -> // return null to signal WeakCache.get() to retry // the loop // 在这里验证supplier是否是Factory实例本身,如果不则返回null让调用者继续轮询重试 // 期间supplier可能替换成了CacheValue,或者由于生成代理类失败被从二级缓存中移除了 return null; } // else still us (supplier == this) // create new value V value = null; try { // 委托valueFactory去生成代理类,这里会通过传入的ProxyClassFactory去生成代理类 // 后面详细讲 ProxyClassFactory 代理类工厂,代理对象就是在这里产生的 value = Objects.requireNonNull(valueFactory.apply(key,parameter)); } finally { if (value == null) { // remove us on failure // 如果生成代理类失败,就将这个二级缓存删除 valuesMap.remove(subKey,this); } } // the only path to reach here is with non-null value // 只有value的值不为空才能到达这里 assert value != null; // wrap value with CacheValue (WeakReference) // 使用弱引用包装生成的代理类 CacheValue<V> cacheValue = new CacheValue<>(value); // put into reverseMap // 将cacheValue成功放入二级缓存后,再对它进行标记 reverseMap.put(cacheValue,Boolean.TRUE); // try replacing us with CacheValue (this should always succeed) // 用缓存包装类替换this,必须成功,否则抛出异常 if (!valuesMap.replace(subKey,this,cacheValue)) { throw new AssertionError("Should not reach here"); } // successfully replaced us with new CacheValue -> return the value // wrapped by it return value; } 最后一个核心方法 valueFactory.apply(key,parameter) 通过该方法就生成了代理类字节码 // 这个代理类工厂是在Proxy类初始化proxyClassCache静态变量时传入的 private static final class ProxyClassFactory implements BiFunction<ClassLoader,Class<?>> { // prefix for all proxy class names // 所有代理类的前缀,我们在debug的时候看到的JDK代理对象都是这样的 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); // 验证接口 // 1. 验证类加载器加载的对象接口是否是同一个 // 2. 验证类对象是否是一个接口 // 3. 验证此接口是否重复 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(); // 代理类的全限定名称:com.sun.proxy.$Proxy0 String proxyName = proxyPkg + proxyClassNamePrefix + num; /* * Generate the specified proxy class. */ // 核心代码,生成代理类的字节码 byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName,interfaces,accessFlags); try { // 把代理类加载到JVM中,至此代理类创建完成了 return defineClass0(loader,proxyName,proxyClassFile,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()); } } } 我们再看看Factory这个内部工厂类,可以看到它的get方法是使用synchronized关键字进行了同步。进行get方法后首先会去验证subKey对应的suppiler是否是工厂本身,如果不是就返回null,而WeakCache的get方法会继续进行重试。如果确实是工厂本身,那么就会委托ProxyClassFactory生成代理类,ProxyClassFactory是在构造WeakCache的时候传入的。所以这里解释了为什么最后会调用到Proxy的ProxyClassFactory这个内部工厂来生成代理类。生成代理类后使用弱引用进行包装并放入reverseMap中,最后会返回原装的代理类 Cglib动态代理敬请期待... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |