jdk的動態代理及爲什麼需要接口[轉載] jdk的動態代理及爲什麼需要接口

jdk的動態代理及爲什麼需要接口

https://blog.csdn.net/zxysshgood/article/details/78684229

動態代理有關,無非是使用JDK動態代理,和cglib動態代理。一直不待明白的是爲什麼,jdk的動態代理需要接口才能實現,這也是其短板和令人詬病的地方。很多的博文說的很複雜,代碼一大堆,沒有太明白。手打了一下,參考了一些優秀的博文,在這裏給自己做個總結。

首先,動態代理是個挺有用的東西,常見的就是javaAOP的有關,主要的作用是是在一個方法使用前後,能進行別的處理。比如吧,aop所說的,面向切面編程,日誌有關,檢測有關。都可以通過AOP或者說動態代理來實現。

先來看下最簡單的代碼下實現動態代理的情況,然後看下源碼和我們的主題,爲什麼需要接口

爲什麼需要接口,先上結論

1.在需要繼承proxy類獲得有關方法和InvocationHandler構造方法傳參的同時,java不能同時繼承兩個類,我們需要和想要代理的類建立聯繫,只能實現一個接口

2.需要反射獲得代理類的有關參數,必須要通過某個類,反射獲取有關方法,如本次測試用的 :printSomeThing

3.成功返回的是object類型,要獲取原類,只能繼承/實現,或者就是那個代理類

4.對具體實現的方法內部並不關心,這個交給InvocationHandler.invoke那個方法裏去處理就好了,我只想根據你給我的接口反射出對我有用的東西。

5.考慮到設計模式,以及proxy編者編寫代碼的邏輯使然


過程:

1.實現簡單的JDK動態代理

1.1爲什麼需要這個接口的---這個接口==

  1. package Activeproxy.jdk;
  2. public interface tagService {
  3. public void printSomeThing();
  4. }

1.2編寫他的簡單實現類

  1. package Activeproxy.jdk;
  2. public class tagServiceImpl implements tagService {
  3. public final void printSomeThing() {
  4. System.err.println("this is printSomeThing Core ServiceImpl");
  5. }
  6. }

1.3編寫調用處理程序InvocationHandler,實現該方法,其實在後面調用的時候,具體方法的調用,會進入這裏面按下面invoke裏面的順序執行。三個參數,分別代表,傳入的代理實現類,此次調用的方法名稱,有關參數。這是平時使用的時候的動態代理的核心方法,看起來也很簡答

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. public class jdkInvocation implements InvocationHandler {
  5. private Object object;
  6. public void setTagServiceObject(Object object) {
  7. this.object = object;
  8. }
  9. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  10. System.out.println("TagService代理前");
  11. Object returnObject = method.invoke(this.object, args);
  12. System.out.println("TagService代理後");
  13. return returnObject;
  14. }
  15. }

1.4編寫調用類

  1. package Activeproxy.jdk;
  2. import java.lang.reflect.Proxy;
  3. public class start {
  4. public static void main(String[] args) {
  5. jdkInvocation invocation = new jdkInvocation();
  6. invocation.setTagServiceObject(new tagServiceImpl());
  7. tagService service = (tagService) Proxy
  8. .newProxyInstance(start.class.getClassLoader(), new Class[] { tagService.class }, invocation);
  9. service.printSomeThing();
  10. }
  11. }

啓動後,控制檯會輸出



就簡單的動態代理來說在這裏就結束了,但是並沒有解決的我的疑問,爲什麼jdk動態代理需要接口

主要的祕密在newProxyInstance這個方法裏

2.1在jdk 1.8裏這個方法的代碼是這樣的

  1. @CallerSensitive
  2. public static Object newProxyInstance(ClassLoader loader,
  3. Class<?>[] interfaces,
  4. InvocationHandler h)
  5. throws IllegalArgumentException
  6. {
  7. Objects.requireNonNull(h);
  8. final Class<?>[] intfs = interfaces.clone();
  9. final SecurityManager sm = System.getSecurityManager();
  10. if (sm != null) {
  11. checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
  12. }
  13. /*
  14. * Look up or generate the designated proxy class.
  15. */
  16. Class<?> cl = getProxyClass0(loader, intfs);
  17. /*
  18. * Invoke its constructor with the designated invocation handler.
  19. */
  20. try {
  21. if (sm != null) {
  22. checkNewProxyPermission(Reflection.getCallerClass(), cl);
  23. }
  24. final Constructor<?> cons = cl.getConstructor(constructorParams);
  25. final InvocationHandler ih = h;
  26. if (!Modifier.isPublic(cl.getModifiers())) {
  27. AccessController.doPrivileged(new PrivilegedAction<Void>() {
  28. public Void run() {
  29. cons.setAccessible(true);
  30. return null;
  31. }
  32. });
  33. }
  34. return cons.newInstance(new Object[]{h});
  35. } catch (IllegalAccessException|InstantiationException e) {
  36. throw new InternalError(e.toString(), e);
  37. } catch (InvocationTargetException e) {
  38. Throwable t = e.getCause();
  39. if (t instanceof RuntimeException) {
  40. throw (RuntimeException) t;
  41. } else {
  42. throw new InternalError(t.toString(), t);
  43. }
  44. } catch (NoSuchMethodException e) {
  45. throw new InternalError(e.toString(), e);
  46. }
  47. }


講解一下:

checkProxyAccess是驗證一些參數

Class<?> cl = getProxyClass0(loader, intfs);是查找或生成指定的代理類(重點)

final Constructor<?> cons = cl.getConstructor(constructorParams);這行代碼是獲取,生成代理類的構造函數是 InvocationHandler參數的方法

return cons.newInstance(new Object[]{h});這行代碼的意思是將h,就是實現InvocationHandler的jdkInvocation注入到cons中。然後newInstance生成一個已經組裝過參數的代理類。

現在這個參數名是cl的代理類,經過,獲得cl,注入InvocationHandler的實現類。通過他newInstance的類,我們可以猜測一下,應該是有一個構造方法可以注入InvocationHandler的實現類。而且,還能被(tagService)這樣強轉,那麼應該是繼承了或者實現了tagService。

so,到這一步,理論上來說我們可以知道:一個代理類,繼承或者實現了我們專門創的接口,且內部有構造方法接受InvocationHandler的實現類。兩個類關聯起來了。而且,如果調用實例化成功的話,我們已經可以通過接口訪問內部的方法了。


那麼重點來了,這個方法裏的重點就是getProxyClass0(loader, intfs);這個方法裏面是什麼樣的,做了哪些操作,返回的是什麼代理類

3.1 getProxyClass0   通過該類源碼,發現其實他是從  proxyClassCache裏獲取的。這個是已經獲取完畢後放在所謂,有關代理的靜態緩存中。怎麼處理的祕密在ProxyClassFactory中

  1. private static Class<?> getProxyClass0(ClassLoader loader,
  2. Class<?>... interfaces) {
  3. if (interfaces.length > 65535) {
  4. throw new IllegalArgumentException("interface limit exceeded");
  5. }
  6. // If the proxy class defined by the given loader implementing
  7. // the given interfaces exists, this will simply return the cached copy;
  8. // otherwise, it will create the proxy class via the ProxyClassFactory
  9. return proxyClassCache.get(loader, interfaces);
  10. }
3.2.ProxyClassFactory
  1. private static final class ProxyClassFactory
  2. implements BiFunction<ClassLoader, Class<?>[], Class<?>>
  3. {
  4. // prefix for all proxy class names
  5. private static final String proxyClassNamePrefix = "$Proxy";
  6. // next number to use for generation of unique proxy class names
  7. private static final AtomicLong nextUniqueNumber = new AtomicLong();
  8. @Override
  9. public Class<?> apply(ClassLoader loader, Class<?>[] interfaces) {
  10. Map<Class<?>, Boolean> interfaceSet = new IdentityHashMap<>(interfaces.length);
  11. for (Class<?> intf : interfaces) {
  12. /*
  13. * Verify that the class loader resolves the name of this
  14. * interface to the same Class object.
  15. */
  16. Class<?> interfaceClass = null;
  17. try {
  18. interfaceClass = Class.forName(intf.getName(), false, loader);
  19. } catch (ClassNotFoundException e) {
  20. }
  21. if (interfaceClass != intf) {
  22. throw new IllegalArgumentException(
  23. intf + " is not visible from class loader");
  24. }
  25. /*
  26. * Verify that the Class object actually represents an
  27. * interface.
  28. */
  29. if (!interfaceClass.isInterface()) {
  30. throw new IllegalArgumentException(
  31. interfaceClass.getName() + " is not an interface");
  32. }
  33. /*
  34. * Verify that this interface is not a duplicate.
  35. */
  36. if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
  37. throw new IllegalArgumentException(
  38. "repeated interface: " + interfaceClass.getName());
  39. }
  40. }
  41. String proxyPkg = null; // package to define proxy class in
  42. int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
  43. /*
  44. * Record the package of a non-public proxy interface so that the
  45. * proxy class will be defined in the same package. Verify that
  46. * all non-public proxy interfaces are in the same package.
  47. */
  48. for (Class<?> intf : interfaces) {
  49. int flags = intf.getModifiers();
  50. if (!Modifier.isPublic(flags)) {
  51. accessFlags = Modifier.FINAL;
  52. String name = intf.getName();
  53. int n = name.lastIndexOf('.');
  54. String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
  55. if (proxyPkg == null) {
  56. proxyPkg = pkg;
  57. } else if (!pkg.equals(proxyPkg)) {
  58. throw new IllegalArgumentException(
  59. "non-public interfaces from different packages");
  60. }
  61. }
  62. }
  63. if (proxyPkg == null) {
  64. // if no non-public proxy interfaces, use com.sun.proxy package
  65. proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
  66. }
  67. /*
  68. * Choose a name for the proxy class to generate.
  69. */
  70. long num = nextUniqueNumber.getAndIncrement();
  71. String proxyName = proxyPkg + proxyClassNamePrefix + num;
  72. /*
  73. * Generate the specified proxy class.
  74. */
  75. byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
  76. proxyName, interfaces, accessFlags);
  77. try {
  78. return defineClass0(loader, proxyName,
  79. proxyClassFile, 0, proxyClassFile.length);
  80. } catch (ClassFormatError e) {
  81. /*
  82. * A ClassFormatError here means that (barring bugs in the
  83. * proxy class generation code) there was some other
  84. * invalid aspect of the arguments supplied to the proxy
  85. * class creation (such as virtual machine limitations
  86. * exceeded).
  87. */
  88. throw new IllegalArgumentException(e.toString());
  89. }
  90. }
  91. }
這麼長的一大串代碼其實也沒幹太多事,無非也就是驗證參數,驗證接口,反射獲得接口。重點是

 byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);

這個方法就是要找到的原因。他隱藏起來了最關鍵的,怎麼組裝了一個byte類型代理類。在後面傳了回去

通過反編譯,我們得知,返回的結果是,

  1. package com.sun.proxy;
  2. import com.Activeproxy.jdk.tagService;
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5. import java.lang.reflect.Proxy;
  6. import java.lang.reflect.UndeclaredThrowableException;
  7. public final class $Proxy0 extends Proxy implements tagService{
  8. private static Method m1;
  9. private static Method m3;
  10. private static Method m0;
  11. private static Method m2;
  12. public $Proxy0(InvocationHandler paramInvocationHandler) {
  13. super(paramInvocationHandler);
  14. }
  15. public final boolean equals(Object paramObject) {
  16. try {
  17. return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
  18. }
  19. catch (Error|RuntimeException localError) {
  20. throw localError;
  21. }
  22. catch (Throwable localThrowable) {
  23. throw new UndeclaredThrowableException(localThrowable);
  24. }
  25. }
  26. public final void printSomeThing(String paramString) {
  27. try {
  28. this.h.invoke(this, m3, new Object[] { paramString });
  29. return;
  30. }
  31. catch (Error|RuntimeException localError) {
  32. throw localError;
  33. }
  34. catch (Throwable localThrowable) {
  35. throw new UndeclaredThrowableException(localThrowable);
  36. }
  37. }
  38. public final int hashCode() {
  39. try {
  40. return ((Integer)this.h.invoke(this, m0, null)).intValue();
  41. }
  42. catch (Error|RuntimeException localError) {
  43. throw localError;
  44. }
  45. catch (Throwable localThrowable) {
  46. throw new UndeclaredThrowableException(localThrowable);
  47. }
  48. }
  49. public final String toString() {
  50. try {
  51. return (String)this.h.invoke(this, m2, null);
  52. }
  53. catch (Error|RuntimeException localError) {
  54. throw localError;
  55. }
  56. catch (Throwable localThrowable) {
  57. throw new UndeclaredThrowableException(localThrowable);
  58. }
  59. }
  60. static {
  61. try {
  62. m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
  63. m3 = Class.forName("com.Activeproxy.jdk.tagService").getMethod("printSomeThing", new Class[0]);
  64. m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
  65. m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
  66. return;
  67. }
  68. catch (NoSuchMethodException localNoSuchMethodException) {
  69. throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
  70. }
  71. catch (ClassNotFoundException localClassNotFoundException) {
  72. throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
  73. }
  74. }
  75. }
看到這個方法,就算破案了。這個就是jdk動態代理和爲什麼需要接口。因爲

1.在需要繼承proxy類獲得有關方法和InvocationHandler構造方法傳參的同時,java不能同時繼承兩個類,我們需要和想要代理的類建立聯繫,只能實現一個接口

2.需要反射獲得代理類的有關參數,必須要通過某個類,反射獲取有關方法,如本次測試用的 :printSomeThing

3.成功返回的是object類型,要獲取原類,只能繼承/實現,或者就是那個代理類

4.對具體實現的方法內部並不關心,這個交給InvocationHandler.invoke那個方法裏去處理就好了,我只想根據你給我的接口反射出對我有用的東西。

5.考慮到設計模式,以及proxy編者編寫代碼的邏輯使然


這就是jdk的動態代理及爲什麼需要接口

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章