动态代理有关,无非是使用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里这个方法的代码是这样的

 

@CallerSensitive
     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);
     }
 3.2.ProxyClassFactory    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) {
                 }
                 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());  
     }  
   }  
 }  
   
   
   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());  
     }  
   }  
 }


看到这个方法,就算破案了。这个就是jdk动态代理和为什么需要接口。因为

 

1.在需要继承proxy类获得有关方法和InvocationHandler构造方法传参的同时,java不能同时继承两个类,我们需要和想要代理的类建立联系,只能实现一个接口

2.需要反射获得代理类的有关参数,必须要通过某个类,反射获取有关方法,如本次测试用的 :printSomeThing

3.成功返回的是object类型,要获取原类,只能继承/实现,或者就是那个代理类

4.对具体实现的方法内部并不关心,这个交给InvocationHandler.invoke那个方法里去处理就好了,我只想根据你给我的接口反射出对我有用的东西。

5.考虑到设计模式,以及proxy编者编写代码的逻辑使然

这就是jdk的动态代理及为什么需要接口