Spring 動態代理

superCoderr發表於2021-05-15

在講動態代理之前,先來了解兩個單詞:

proxy 代理
invocationHandler 呼叫處理程式

不太懂,再去扒一下JDK文件;

Spring 動態代理
再來看下官方解釋:

  • InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
  • 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 invoke method of its invocation handler.

Spring 動態代理
介面下只有一個方法:invoke
方法中有三個引數:invoke(Object proxy, Method method, Object[] args)

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.
Parameters:

  • proxy - the proxy instance that the method was invoked on 。即我要代理的物件
  • method - the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the 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. 即我的代理例項要代理的方法
  • args - an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean. 方法裡要傳遞的引數

再來看下Proxy :

Spring 動態代理

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.

提供了建立動態代理類和例項的靜態方法;

來看下類下面的方法:

Spring 動態代理

newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
翻譯:返回一個特定介面的代理類的例項,該介面分派方法呼叫給特定的呼叫處理程式;

Spring 動態代理

Parameters:
loader - the class loader to define the proxy class :定義代理類的類載入器;
interfaces - the list of interfaces for the proxy class to implement :代理類要實現的介面;
h - the invocation handler to dispatch method invocations to
排程方法呼叫的排程處理程式;

Spring 動態代理

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//用這個類,來自動生成代理類;
public class ProxyInvocationHandler01 implements InvocationHandler {

    //被代理的介面;
  private UserService userService;

  public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
  }

    /**
 *  Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), *                                           new Class<?>[] { Foo.class },
 *                                           handler); */
  //得到代理類,和官方保持一致;
  public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
  userService.getClass().getInterfaces(),this);
  }

    /**
 * 官方解釋:Processes a method invocation on a proxy instance and returns the result.
 * 翻譯:處理代理例項,並返回結果;
  * 但是真正生成代理類的是: proxy
 * 官方已給出瞭解釋:
  * Proxy provides static methods for creating dynamic proxy classes and instances;
  * @param proxy
  * @param method
  * @param args
  * @return
  * @throws Throwable
  */
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //動態代理的本質就是使用反射來實現;
  Object result = method.invoke(userService, args);
  return result;
  }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章