MyBatis+Spring基於接口編程的原理分析

  對於整合Spring及Mybatis不作詳細介紹,可以參考: MyBatis 3 User Guide Simplified Chinese.pdf,貼出我的主要代碼如下: 
UserMapper Interface: 
Java代碼  收藏代碼
  1. package org.denger.mapper;  
  2.   
  3. import org.apache.ibatis.annotations.Param;  
  4. import org.apache.ibatis.annotations.Select;  
  5. import org.denger.po.User;  
  6.   
  7. public interface UserMapper {  
  8.   
  9.     @Select("select * from tab_uc_account where id=#{userId}")  
  10.     User getUser(@Param("userId") Long userId);  
  11. }  


application-context.xml: 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  6.   
  7.   
  8.     <!-- Provided by annotation-based configuration  -->  
  9.     <context:annotation-config/>  
  10.   
  11.     <!--JDBC Transaction  Manage -->  
  12.     <bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">  
  13.         <constructor-arg>  
  14.             <ref bean="dataSource" />  
  15.         </constructor-arg>  
  16.     </bean>  
  17.   
  18.     <!-- The JDBC c3p0 dataSource bean-->  
  19.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  20.         <property name="driverClass" value="com.mysql.jdbc.Driver" />  
  21.         <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/noah" />  
  22.         <property name="user" value="root" />  
  23.         <property name="password" value="123456" />  
  24.     </bean>  
  25.   
  26.     <!--MyBatis integration with Spring as define sqlSessionFactory  -->  
  27.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  28.         <property name="dataSource" ref="dataSource" />  
  29.     </bean>  
  30.   
  31.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  32.         <property name="sqlSessionFactory"  ref="sqlSessionFactory"/>  
  33.         <property name="basePackage" value="org.denger.mapper"></property>  
  34.     </bean>  
  35. </beans>  

test Class: 
Java代碼  收藏代碼
  1. package org.denger.mapper;  
  2.   
  3. import org.junit.Assert;  
  4. import org.junit.Test;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.test.context.ContextConfiguration;  
  7. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;  
  8.   
  9. @ContextConfiguration(locations = { "/application-context.xml"})  
  10. public class UserMapperTest extends AbstractJUnit4SpringContextTests{  
  11.   
  12.     @Autowired  
  13.     public UserMapper userMapper;  
  14.       
  15.     @Test  
  16.     public void testGetUser(){  
  17.         Assert.assertNotNull(userMapper.getUser(300L));  
  18.     }  
  19. }  


實現原理分析 
     對於以上極其簡單代碼看上去並無特殊之處,主要亮點在於 UserMapper 居然不用實現類,而且在調用 getUser 的時候,也是使用直接調用了UserMapper實現類,那麼Mybatis是如何去實現 UserMapper的接口的呢? 
    可能你馬上能想到的實現機制就是通過動態代理方式,好吧,看看MyBatis整個的代理過程吧。 
    
    首先在Spring的配置文件中看到下面的Bean: 
   
Xml代碼  收藏代碼
  1. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  2.         <property name="sqlSessionFactory"  ref="sqlSessionFactory"/>  
  3.         <property name="basePackage" value="org.denger.mapper"></property>  
  4.     </bean>  

   以上的MapperScannerConfigurer class的註釋中描述道: 
   從base 包中搜索所有下面所有 interface,並將其註冊到 Spring Bean容器中,其註冊的class bean是MapperFactoryBean。 
   好吧,看看它的註冊過程,下面方法來從 MapperScannerConfigurer中的Scanner類中抽取,下面方法在初始化以上application-content.xml文件時就會進行調用。 主要用於是搜索 base packages 下的所有mapper class,並將其註冊至 spring 的 benfinitionHolder中。 
  
Java代碼  收藏代碼
  1. /** 
  2. * Calls the parent search that will search and register all the candidates. Then the 
  3. * registered objects are post processed to set them as MapperFactoryBeans 
  4. */  
  5. @Override  
  6. protected Set<BeanDefinitionHolder> doScan(String... basePackages) {  
  7.         //#1  
  8.     Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);  
  9.     if (beanDefinitions.isEmpty()) {  
  10.             logger.warn("No MyBatis mapper was found in '" + MapperScannerConfigurer.this.basePackage  
  11.                         + "' package. Please check your configuration.");  
  12.     } else {  
  13.          //#2  
  14.          for (BeanDefinitionHolder holder : beanDefinitions) {  
  15.                 GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();  
  16.             if (logger.isDebugEnabled()) {  
  17.                 logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '"+ definition.getBeanClassName() + "' mapperInterface");  
  18.             }  
  19.            //#3  
  20.           definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());  
  21.           definition.setBeanClass(MapperFactoryBean.class);  
  22.      }  
  23.     return beanDefinitions;  
  24. }  
    #1: 瞭解Spring初始化Bean過程的人可能都知道,Spring 首先會將需要初始化的所有class先通過BeanDefinitionRegistry進行註冊,並且將該Bean的一些屬性信息(如scope、className、beanName等)保存至BeanDefinitionHolder中;Mybatis這裏首先會調用Spring中的ClassPathBeanDefinitionScanner.doScan方法,將所有Mapper接口的class註冊至BeanDefinitionHolder實例中,然後返回一個Set<BeanDefinitionHolder>,其它包含了所有搜索到的Mapper class BeanDefinitionHolder對象。 

    #2: 首先,由於 #1:  中註冊的都是接口class, 可以肯定的是接口是不能直接初始化的;實際 #2: 中for循環中替換當前所有 holder的 className爲 MapperFactoryBean.class,並且將 mapper interface的class name setter 至 MapperFactoryBean 屬性爲 mapperInterface 中,也就是 #3: 代碼所看到的。 
  再看看 MapperFactoryBean,它是直接實現了 Spring 的 FactoryBean及InitializingBean 接口。其實既使不看這兩個接口,當看MapperFactoryBean的classname就知道它是一個專門用於創建 Mapper 實例Bean的工廠。 

  至此,已經完成了Spring與mybatis的整合的初始化配置的過程。 

    接着,當我在以上的 Test類中,需要注入 UserMapper接口實例時,由於mybatis給所有的Mapper 實例註冊都是一個MapperFactory的工廠,所以產生UserMapper實現仍需要 MapperFactoryBean來進行創建。接下來看看 MapperFactoryBean的處理過程。 
    先需要創建Mapper實例時,首先在 MapperFactoryBean中執行的方法是: 
 
Java代碼  收藏代碼
  1. /** 
  2.      * {@inheritDoc} 
  3.      */  
  4.     public void afterPropertiesSet() throws Exception {  
  5.         Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required");  
  6.         Assert.notNull(this.mapperInterface, "Property 'mapperInterface' is required");  
  7.   
  8.         Configuration configuration = this.sqlSession.getConfiguration();  
  9.         if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {  
  10.             configuration.addMapper(this.mapperInterface);  
  11.         }  
  12.     }  
    上面方法中先會檢測當前需要創建的 mapperInterface在全局的 Configuration中是否存在,如果不存在則添加。呆會再說他爲什麼要將 mapperInterface 添加至 Configuration中。該方法調用完成之後,就開始調用 getObject方法來產生mapper實例,看到MapperFactoryBean.getObject的該方法代碼如下: 
  
Java代碼  收藏代碼
  1. /** 
  2.     * {@inheritDoc} 
  3.     */  
  4.    public T getObject() throws Exception {  
  5.        return this.sqlSession.getMapper(this.mapperInterface);  
  6.    }  
    通過Debug可以看到 sqlSession及mapperInterface對象: 
 
    到目前爲止我們的 UserMapper實例實際上還並未產生; 再進入org.mybatis.spring.SqlSessionTemplate中的getMapper方法,該方法將 this及mapper interface class 作爲參數傳入 org.apache.ibatis.session.Configuration的getMapper() 方法中,代碼如下: 
     
Java代碼  收藏代碼
  1. /** 
  2.      * {@inheritDoc} 
  3.      */  
  4.     public <T> T getMapper(Class<T> type) {  
  5.         return getConfiguration().getMapper(type, this);  
  6.     }  

   於是,再進入 org.apache.ibatis.session.Configuration.getMapper中代碼如下: 
   
Java代碼  收藏代碼
  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {  
  2.     return mapperRegistry.getMapper(type, sqlSession);  
  3.   }  
    其中 mapperRegistry保存着當前所有的 mapperInterface class. 那麼它在什麼時候將 mapperinterface class 保存進入的呢?其實就是在上面的 afterPropertiesSet 中通過 configuration.addMapper(this.mapperInterface) 添加進入的。 
    再進入 org.apache.ibatis.binding.MapperRegistry.getMapper方法,代碼如下:
Java代碼  收藏代碼
  1. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {  
  2.   //首先判斷當前knownMappers是否存在mapper interface class.因爲前面說到 afterPropertiesSet 中已經將當前的 mapperinterfaceclass 添加進入了。  
  3.     if (!knownMappers.contains(type))  
  4.       throw new BindingException("Type " + type + " is not known to the MapperRegistry.");  
  5.     try {  
  6.       return MapperProxy.newMapperProxy(type, sqlSession);  
  7.     } catch (Exception e) {  
  8.       throw new BindingException("Error getting mapper instance. Cause: " + e, e);  
  9.     }  
  10.   }  

    嗯,沒錯,看到 MapperProxy.newMapperProxy後可以肯定的是它確實採用的代理模式,再進入一看究竟吧: 
  
Java代碼  收藏代碼
  1. public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {  
  2.     ClassLoader classLoader = mapperInterface.getClassLoader();  
  3.     Class[] interfaces = new Class[]{mapperInterface};  
  4.     MapperProxy proxy = new MapperProxy(sqlSession);  
  5.     return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);  
  6.   }  

    
    JDK的動態代理就不說了,至此,基本Mybatis已經完成了Mapper實例的整個創建過程,也就是你在具體使用 UserMapper.getUser 時,它實際上調用的是 MapperProxy,因爲此時 所返回的 MapperProxy是實現了 UserMapper接口的。只不過 MapperProxy攔截了所有對userMapper中方法的調用,如下: 
 
Java代碼  收藏代碼
  1. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  2.    try {  
  3.     //如果調用不是 object 中默認的方法(如equals之類的)  
  4.      if (!OBJECT_METHODS.contains(method.getName())) {  
  5.        //因爲當前MapperProxy代理了所有 Mapper,所以他需要根據當前攔截到的方法及代理對象獲取 MapperInterface  class,也就是我這裏的 UserMapper.class  
  6.        final Class declaringInterface = findDeclaringInterface(proxy, method);  
  7.        //然後再根據UserMapper.class、及當前調用的Method(也就是getUser)及SqlSession構造一個 MapperMethod,在這裏面會獲取到 getUser方法上的 @Select() 的SQL,然後再通過 sqlSession來進行執行  
  8.        final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);  
  9.        //execute執行數據庫操作,並返回結果集  
  10.        final Object result = mapperMethod.execute(args);  
  11.        if (result == null && method.getReturnType().isPrimitive()) {  
  12.          throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");  
  13.        }  
  14.        return result;  
  15.      }  
  16.    } catch (SQLException e) {  
  17.      e.printStackTrace();  
  18.    }  
  19.    return null;  
  20.  }  

     爲什麼說它攔截了呢?可以看到, 它並沒有調用 method.invoke(object)方法,因爲實際上 MapperProxy只是動態的 implement 了UserMapper接口,但它沒有真正實現 UserMapper中的任何方法。至於結果的返回,它也是通過 MapperMethod.execute 中進行數據庫操作來返回結果的。 說白了,就是一個實現了 MapperInterface 的 MapperProxy 實例被MapperProxy代理了,可以debug看看 userMapper實例就是 MapperProxy。


原文:http://denger.iteye.com/blog/1060588

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章