3

静态代理、动态代理与Mybatis的理解 - lovekey

 2 years ago
source link: https://www.cnblogs.com/lovekey/p/16395086.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

静态代理、动态代理与Mybatis的理解

这里的代理与设计模式中的代理模式密切相关,代理模式的主要作用是为其他对象提供一种控制对这个对象的访问方法,即在一个对象不适合或者不能直接引用另一个对象时,代理对象充当中介的作用。

现实生活中比较贴切的例子比如租房,被代理对象就是房东,代理对象就是中介,使用者就是租客,租客通过中介向房东租赁房屋,即使用者通过代理对象访问被代理对象。

一、直接调用

  • 一般我们通过new关键字初始化对象来调用类中的方法

  • 如下代码,创建Human接口,Student类实现了Human接口,在main函数中,通过new关键字来初始化Student对象来实现对Student类中say()方法的调用

interface Human{ public void say();} class Student implements Human{ @Override public void say() { System.out.println("I'm a Student"); }} public class ProxyTest { public static void main(String[] args) { Human human = new Student(); human.say(); }} //输出//I'm a Student

二、静态代理

实现静态代理有以下三个步骤:

  • 创建接口,通过接口来实现对象的代理

  • 创建该接口的实现类

  • 创建Proxy代理类来调用我们需要的方法

interface Human{ public void say();} class Student implements Human{ @Override public void say() { System.out.println("I'm a Student"); }} class StudentProxy implements Human{ private Student student; public StudentProxy(){} public StudentProxy(Student student){ this.student = student; } private void begin(){ System.out.println("Begin"); } private void end(){ System.out.println("End"); } @Override public void say() { begin(); student.say(); end(); }} public class ProxyTest { public static void main(String[] args) { Student student = new Student(); StudentProxy studentProxy = new StudentProxy(student); studentProxy.say(); }} //输出//Begin//I'm a Student//End

在上述代码中,我们在没有修改Student类中say()方法的情况下,实现了在原来的say()方法前后分别执行sayHello()sayBye()方法。由此引出代理模式的主要作用:

  • 在不修改被代理对象的情况下,实现对被代理对象功能的增强

同时,静态代理也存在一些比较致命的缺点。想象这样一个场景:若新增一个Worker类实现了Human接口,我们应该如何去代理这个Worker类?比较容易想到的方法是扩大StudentProxy的代理范围,然后将Worker当作参数传入StudentProxy,然后继续使用StudentProxy类代理Worker对象。这样实现功能是没有问题的,但会存在如下问题:

  • 当Human接口的实例中方法增加时,代理类中代码会变得非常冗长
  • 当有其他不属于Human类的子类需要被代理时,需要新增一个新的代理类

由此引出动态代理

三、动态代理

使用动态代理时,我们不需要编写实现类,而是通过JDK提供的Proxy.newProxyInstance()创建一个Human接口的对象。

生成动态代理有以下几个步骤:

  • 定义一个InvocationHandler实例,它负责实现接口的方法调用;
  • 通过Proxy.newProxyInstance()创建interface实例,它需要3个参数:
    • 使用的ClassLoader,通常是接口类的ClassLoader
    • 需要实现的接口数组,至少需要传入一个接口进去;
    • 用来处理接口方法调用的InvocationHandler实例。
  • 将返回的Object强制转型为接口。
interface Human{ public void say();} class Student implements Human{ @Override public void say() { System.out.println("I'm a Student"); } @Override public void eat() { System.out.println("I eat something"); }} class MyInvocationHandler implements InvocationHandler { private Object object; public MyInvocationHandler(){} public MyInvocationHandler(Object object){ this.object = object; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Begin"); Object invoke = method.invoke(object, args); System.out.println("End"); return invoke; }} public class ProxyTest { public static void main(String[] args) { MyInvocationHandler handler = new MyInvocationHandler(new Student()); Human human = (Human) Proxy.newProxyInstance( Human.class.getClassLoader(), new Class[] {Human.class}, handler); human.say(); human.eat(); }}

当Human接口的实例中方法增加时,如新增eat()方法时,只需要在Student类中直接实例化该方法即可。

当有其他不属于Human类的子类需要被代理时,只需要将传入MyInvocationHandler()中的new Student()替换为需要被代理的子类即可。

综上所述,通过动态代理基本可以解决静态代理的痛点。

四、Mybatis中的动态代理

在Springboot项目中配置Mybatis时,我们仅编写了Mapper接口,并未编写Mapper接口的实现类,那么当我们调用Mapper接口中方法时,是如何生成方法体的呢?

首先,项目在启动时生成MapperFactoryBean对象,通过factory.getObject()方法获取mapper的代理对象

2742789-20220620223647752-535337700.png
2742789-20220620223657734-660293727.png
2742789-20220620223707984-1564332160.png
2742789-20220620223713485-74015989.png
2742789-20220620223722666-1393803509.png

将上述过程与动态代理的步骤进行对比,我们最终获取的是一个类似于动态代理例子中Human的代理对象,这里是MapperProxy的代理对象。至此,一个Mapper代理对象就生成完毕。

然后,当我们完成项目中Mybatis的相关配置后,使用我们Mapper接口中的数据库相关方法时,将调用之前生成的MapperProxy代理对象中invoke()方法。类比动态代理的例子,即调用MyInvocationHandler类中的invoke()方法。

2742789-20220620223741066-879537065.png
//83行代码含义:如果method为Object中定义的方法(toString()、hash()...)则直接执行,这里我们要执行的是Mapper接口中定义的方法,显然返回为falseObject.class.equals(method.getDeclaringClass())

于是执行cachedInvoker(method)invoke()方法

2742789-20220620223754436-41632634.png

进入execute()方法,我们看到之前我们配置的mapper.xml在MapperMethod初始化时,被解析成了59行的command。在此处通过sqlSession对象实现了对数据库的操作。

2742789-20220620223836755-1668080002.png

至此,我们对Mybatis的数据库操作流程已经有了大致了解。回到开头的问题:为什么仅编写了Mapper接口,并未编写Mapper接口的实现类,仍然可以实现我们的功能?这与我们之前的动态代理例子有什么区别呢?

研究代码我们发现,我们并没有直接使用method.invoke()方法来调用实现类中的方法,而是调用了cachedInvoker(method)invoke()方法解析我们配置的Mapper.xml,并通过sqlSession实现了数据库操作,这个invoke()方法相当于Mybatis自定义的方法。因此,这里的invoke()方法具体执行的逻辑是根据Mapper.xml配置来生成的,这个Mapper.xml配置可以理解为Mapper接口的实现类。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK