Spring 动态代理

Java
467
0
0
2022-04-12
标签   SpringBoot

在讲动态代理之前,先来了解两个单词:

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
  */@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //动态代理的本质就是使用反射来实现;Object result = method.invoke(userService, args);return result;}
}