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爲什麼需要這個接口的---這個接口==
- package Activeproxy.jdk;
- public interface tagService {
- public void printSomeThing();
- }
1.2編寫他的簡單實現類
- package Activeproxy.jdk;
- public class tagServiceImpl implements tagService {
- public final void printSomeThing() {
- System.err.println("this is printSomeThing Core ServiceImpl");
- }
- }
1.3編寫調用處理程序InvocationHandler,實現該方法,其實在後面調用的時候,具體方法的調用,會進入這裏面按下面invoke裏面的順序執行。三個參數,分別代表,傳入的代理實現類,此次調用的方法名稱,有關參數。這是平時使用的時候的動態代理的核心方法,看起來也很簡答
- package Activeproxy.jdk;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- public class jdkInvocation implements InvocationHandler {
- private Object object;
- public void setTagServiceObject(Object object) {
- this.object = object;
- }
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- System.out.println("TagService代理前");
- Object returnObject = method.invoke(this.object, args);
- System.out.println("TagService代理後");
- return returnObject;
- }
- }
1.4編寫調用類
- package Activeproxy.jdk;
- import java.lang.reflect.Proxy;
- public class start {
- public static void main(String[] args) {
- jdkInvocation invocation = new jdkInvocation();
- invocation.setTagServiceObject(new tagServiceImpl());
- tagService service = (tagService) Proxy
- .newProxyInstance(start.class.getClassLoader(), new Class[] { tagService.class }, invocation);
- service.printSomeThing();
- }
- }
啓動後,控制檯會輸出
就簡單的動態代理來說在這裏就結束了,但是並沒有解決的我的疑問,爲什麼jdk動態代理需要接口
主要的祕密在newProxyInstance這個方法裏
2.1在jdk 1.8裏這個方法的代碼是這樣的
- public static Object newProxyInstance(ClassLoader loader,
- Class<?>[] interfaces,
- InvocationHandler h)
- throws IllegalArgumentException
- {
- Objects.requireNonNull(h);
- final Class<?>[] intfs = interfaces.clone();
- final SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
- }
- /*
- * Look up or generate the designated proxy class.
- */
- Class<?> cl = getProxyClass0(loader, intfs);
- /*
- * Invoke its constructor with the designated invocation handler.
- */
- try {
- if (sm != null) {
- checkNewProxyPermission(Reflection.getCallerClass(), cl);
- }
- final Constructor<?> cons = cl.getConstructor(constructorParams);
- final InvocationHandler ih = h;
- if (!Modifier.isPublic(cl.getModifiers())) {
- AccessController.doPrivileged(new PrivilegedAction<Void>() {
- public Void run() {
- cons.setAccessible(true);
- return null;
- }
- });
- }
- return cons.newInstance(new Object[]{h});
- } catch (IllegalAccessException|InstantiationException e) {
- throw new InternalError(e.toString(), e);
- } catch (InvocationTargetException e) {
- Throwable t = e.getCause();
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- } else {
- throw new InternalError(t.toString(), t);
- }
- } catch (NoSuchMethodException e) {
- throw new InternalError(e.toString(), e);
- }
- }
講解一下:
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中
- 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);
- }
- 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();
- 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) {
- }
- 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());
- }
- }
- }
byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags);
這個方法就是要找到的原因。他隱藏起來了最關鍵的,怎麼組裝了一個byte類型代理類。在後面傳了回去
通過反編譯,我們得知,返回的結果是,
- package com.sun.proxy;
- import com.Activeproxy.jdk.tagService;
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Method;
- import java.lang.reflect.Proxy;
- import java.lang.reflect.UndeclaredThrowableException;
- public final class $Proxy0 extends Proxy implements tagService{
- private static Method m1;
- private static Method m3;
- private static Method m0;
- private static Method m2;
- public $Proxy0(InvocationHandler paramInvocationHandler) {
- super(paramInvocationHandler);
- }
- public final boolean equals(Object paramObject) {
- try {
- return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
- }
- catch (Error|RuntimeException localError) {
- throw localError;
- }
- catch (Throwable localThrowable) {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final void printSomeThing(String paramString) {
- try {
- this.h.invoke(this, m3, new Object[] { paramString });
- return;
- }
- catch (Error|RuntimeException localError) {
- throw localError;
- }
- catch (Throwable localThrowable) {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final int hashCode() {
- try {
- return ((Integer)this.h.invoke(this, m0, null)).intValue();
- }
- catch (Error|RuntimeException localError) {
- throw localError;
- }
- catch (Throwable localThrowable) {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- public final String toString() {
- try {
- return (String)this.h.invoke(this, m2, null);
- }
- catch (Error|RuntimeException localError) {
- throw localError;
- }
- catch (Throwable localThrowable) {
- throw new UndeclaredThrowableException(localThrowable);
- }
- }
- static {
- try {
- m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
- m3 = Class.forName("com.Activeproxy.jdk.tagService").getMethod("printSomeThing", new Class[0]);
- m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
- m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
- return;
- }
- catch (NoSuchMethodException localNoSuchMethodException) {
- throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
- }
- catch (ClassNotFoundException localClassNotFoundException) {
- throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
- }
- }
- }
1.在需要繼承proxy類獲得有關方法和InvocationHandler構造方法傳參的同時,java不能同時繼承兩個類,我們需要和想要代理的類建立聯繫,只能實現一個接口
2.需要反射獲得代理類的有關參數,必須要通過某個類,反射獲取有關方法,如本次測試用的 :printSomeThing
3.成功返回的是object類型,要獲取原類,只能繼承/實現,或者就是那個代理類
4.對具體實現的方法內部並不關心,這個交給InvocationHandler.invoke那個方法裏去處理就好了,我只想根據你給我的接口反射出對我有用的東西。
5.考慮到設計模式,以及proxy編者編寫代碼的邏輯使然
這就是jdk的動態代理及爲什麼需要接口