Spring與Mybatis整合的MapperScannerConfigurer處理過程源碼分析


前言

本文將分析mybatis與spring整合的MapperScannerConfigurer的底層原理,之前已經分析過java中實現動態,可以使用jdk自帶api和cglib第三方庫生成動態代理。本文分析的mybatis版本3.2.7,mybatis-spring版本1.2.2。

MapperScannerConfigurer介紹

MapperScannerConfigurer是spring和mybatis整合的mybatis-spring jar包中提供的一個類。

想要了解該類的作用,就得先了解MapperFactoryBean

MapperFactoryBean的出現為了代替手工使用SqlSessionDaoSupport或SqlSessionTemplate編寫數據訪問對象(DAO)的代碼,使用動態代理實現。

比如下面這個官方文檔中的配置:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
  <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
  <property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

org.mybatis.spring.sample.mapper.UserMapper是一個接口,我們創建一個MapperFactoryBean實例,然后注入這個接口和sqlSessionFactory(mybatis中提供的SqlSessionFactory接口,MapperFactoryBean會使用SqlSessionFactory創建SqlSession)這兩個屬性。

之后想使用這個UserMapper接口的話,直接通過spring注入這個bean,然后就可以直接使用了,spring內部會創建一個這個接口的動態代理。

當發現要使用多個MapperFactoryBean的時候,一個一個定義肯定非常麻煩,於是mybatis-spring提供了MapperScannerConfigurer這個類,它將會查找類路徑下的映射器並自動將它們創建成MapperFactoryBean。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

這段配置會掃描org.mybatis.spring.sample.mapper下的所有接口,然后創建各自接口的動態代理類。

MapperScannerConfigurer底層代碼分析

以以下代碼為示例進行講解(部分代碼,其他代碼及配置省略):

package org.format.dynamicproxy.mybatis.dao;
public interface UserDao {
    public User getById(int id);
    public int add(User user);    
    public int update(User user);    
    public int delete(User user);    
    public List<User> getAll();    
}

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.format.dynamicproxy.mybatis.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

我們先通過測試用例debug查看userDao的實現類到底是什么。

我們可以看到,userDao是1個MapperProxy類的實例。
看下MapperProxy的源碼,沒錯,實現了InvocationHandler,說明使用了jdk自帶的動態代理。

public class MapperProxy<T> implements InvocationHandler, Serializable {

  private static final long serialVersionUID = -6424540398559729838L;
  private final SqlSession sqlSession;
  private final Class<T> mapperInterface;
  private final Map<Method, MapperMethod> methodCache;

  public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) {
    this.sqlSession = sqlSession;
    this.mapperInterface = mapperInterface;
    this.methodCache = methodCache;
  }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (Object.class.equals(method.getDeclaringClass())) {
      try {
        return method.invoke(this, args);
      } catch (Throwable t) {
        throw ExceptionUtil.unwrapThrowable(t);
      }
    }
    final MapperMethod mapperMethod = cachedMapperMethod(method);
    return mapperMethod.execute(sqlSession, args);
  }

  private MapperMethod cachedMapperMethod(Method method) {
    MapperMethod mapperMethod = methodCache.get(method);
    if (mapperMethod == null) {
      mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
      methodCache.put(method, mapperMethod);
    }
    return mapperMethod;
  }

}

下面開始分析MapperScannerConfigurer的源碼

MapperScannerConfigurer實現了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor接口是一個可以修改spring工長中已定義的bean的接口,該接口有個postProcessBeanDefinitionRegistry方法。

然后我們看下ClassPathMapperScanner中的關鍵是如何掃描對應package下的接口的。

其實MapperScannerConfigurer的作用也就是將對應的接口的類型改造為MapperFactoryBean,而這個MapperFactoryBean的屬性mapperInterface是原類型。MapperFactoryBean本文開頭已分析過。

所以最終我們還是要分析MapperFactoryBean的實現原理!

MapperFactoryBean繼承了SqlSessionDaoSupport類,SqlSessionDaoSupport類繼承DaoSupport抽象類,DaoSupport抽象類實現了InitializingBean接口,因此實例個MapperFactoryBean的時候,都會調用InitializingBean接口的afterPropertiesSet方法。

DaoSupport的afterPropertiesSet方法:

MapperFactoryBean重寫了checkDaoConfig方法:

然后通過spring工廠拿對應的bean的時候:

這里的SqlSession是SqlSessionTemplate,SqlSessionTemplate的getMapper方法:

Configuration的getMapper方法,會使用MapperRegistry的getMapper方法:

MapperRegistry的getMapper方法:

MapperProxyFactory構造MapperProxy:

沒錯! MapperProxyFactory就是使用了jdk組帶的Proxy完成動態代理。
MapperProxy本來一開始已經提到。MapperProxy內部使用了MapperMethod類完成方法的調用:

下面,我們以UserDao的getById方法來debug看看MapperMethod的execute方法是如何走的。

@Test
public void testGet() {
    int id = 1;
    System.out.println(userDao.getById(id));
}
<select id="getById" parameterType="int" resultType="org.format.dynamicproxy.mybatis.bean.User">
    SELECT * FROM users WHERE id = #{id}
</select>


示例代碼:https://github.com/fangjian0423/dynamic-proxy-mybatis-study

總結

來到了新公司,接觸了Mybatis,以前接觸過~ 但是接觸的不深入,突然發現spring與mybatis整合之后可以只寫個接口而不實現,spring默認會幫我們實現,然后覺得非常神奇,於是寫了篇java動態代碼淺析和本文。

參考資料

https://mybatis.github.io/spring/zh/mappers.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM