天天看點

【MyBatis源碼分析】objectFactory解析屬性配置元素詳述

objectFactory解析、

那麼,接下來,就簡單介紹一下這幾個配置的作用吧:

1、objectFactory是幹什麼的? 需要配置嗎?

  MyBatis 每次建立結果對象的新執行個體時,它都會使用一個對象工廠(ObjectFactory)執行個體來完成。預設的對象工廠需要做的僅僅是執行個體化目标類,要麼通過預設構造方法,要麼在參數映射存在的時候通過參數構造方法來執行個體化。預設情況下,我們不需要配置,mybatis會調用預設實作的objectFactory。 除非我們要自定義ObjectFactory的實作, 那麼我們才需要去手動配置。

      簡單點就是說,這個類其實是為了在對查詢結果集中擷取資料被封裝成所希望的Java實體類型時用到的,使用這個工廠類通過反射的方式來進行執行個體對象的建立。所有的工廠類都必須實作ObjectFactory接口。我們來看看這個接口的定義:

public interface ObjectFactory {

  /**
   * Sets configuration properties.
   * @param properties configuration properties
   */
  void setProperties(Properties properties);

  /**
   * Creates a new object with default constructor. 
   * @param type Object type
   * @return
   */
  <T> T create(Class<T> type);

  /**
   * Creates a new object with the specified constructor and params.
   * @param type Object type
   * @param constructorArgTypes Constructor argument types
   * @param constructorArgs Constructor argument values
   * @return
   */
  <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs);
  
  /**
   * Returns true if this object can have a set of other objects.
   * It's main purpose is to support non-java.util.Collection objects like Scala collections.
   * 
   * @param type Object type
   * @return whether it is a collection or not
   * @since 3.1.0
   */
  <T> boolean isCollection(Class<T> type);

}
           

從這個接口定義可以看出,它包含了兩種通過反射機制構造實體類對象的方法,一種是通過無參構造函數,一種是通過帶參數的構造函數。另外,為了使的這個工廠類能接收設定的附帶屬性,還提供了setProperties()方法。mybatis為我們實作了一個預設實作,那就是DefaultObjectFactory,我們來看看預設是怎麼實作的。

public class DefaultObjectFactory implements ObjectFactory, Serializable {

  private static final long serialVersionUID = -8855120656740914948L;

  @Override
  public <T> T create(Class<T> type) {
    return create(type, null, null);
  }

  @SuppressWarnings("unchecked")
  @Override
  public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    Class<?> classToCreate = resolveInterface(type);
    // we know types are assignable
    return (T) instantiateClass(classToCreate, constructorArgTypes, constructorArgs);
  }

  @Override
  public void setProperties(Properties properties) {
    // no props for default
  }

  <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
    try {
      Constructor<T> constructor;
      if (constructorArgTypes == null || constructorArgs == null) {
        constructor = type.getDeclaredConstructor();
        if (!constructor.isAccessible()) {
          constructor.setAccessible(true);
        }
        return constructor.newInstance();
      }
      constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[constructorArgTypes.size()]));
      if (!constructor.isAccessible()) {
        constructor.setAccessible(true);
      }
      return constructor.newInstance(constructorArgs.toArray(new Object[constructorArgs.size()]));
    } catch (Exception e) {
      StringBuilder argTypes = new StringBuilder();
      if (constructorArgTypes != null && !constructorArgTypes.isEmpty()) {
        for (Class<?> argType : constructorArgTypes) {
          argTypes.append(argType.getSimpleName());
          argTypes.append(",");
        }
        argTypes.deleteCharAt(argTypes.length() - 1); // remove trailing ,
      }
      StringBuilder argValues = new StringBuilder();
      if (constructorArgs != null && !constructorArgs.isEmpty()) {
        for (Object argValue : constructorArgs) {
          argValues.append(String.valueOf(argValue));
          argValues.append(",");
        }
        argValues.deleteCharAt(argValues.length() - 1); // remove trailing ,
      }
      throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
    }
  }

  protected Class<?> resolveInterface(Class<?> type) {
    Class<?> classToCreate;
    if (type == List.class || type == Collection.class || type == Iterable.class) {
      classToCreate = ArrayList.class;
    } else if (type == Map.class) {
      classToCreate = HashMap.class;
    } else if (type == SortedSet.class) { // issue #510 Collections Support
      classToCreate = TreeSet.class;
    } else if (type == Set.class) {
      classToCreate = HashSet.class;
    } else {
      classToCreate = type;
    }
    return classToCreate;
  }

  @Override
  public <T> boolean isCollection(Class<T> type) {
    return Collection.class.isAssignableFrom(type);
  }

}
           

從上面的源碼來看,建立執行個體對象最終都是通過instantiateClass()方法能實作的,在這個方法中,擷取實體類的無參構造函數或者帶參構造函數,然後采用反射的機制來執行個體化實體類對象出來。

如果我們想定義一個自定義的對象工廠類,我們可以實作ObjectFactory這個接口,但是這樣我們就需要自己去實作一些在DefaultObjectFactory已經實作好了的東西,是以如果想自定義一個,可以繼承這個DefaultObjectFactory類,這樣可以使得實作起來更為簡單。

下面我們來舉個示例,我們假設需要自定義一個對象工廠類ExampleObjectFactory,在要建立我之前定義的User對象時會給屬性字段author指派為我的名字,接下來我們看看怎麼實作。

首先我們定義實作這個對象工廠類:

public class ExampleObjectFactory extends DefaultObjectFactory{
	
	private static final long serialVersionUID = 3608715667301891724L;  
	
	@Override  
    public <T> T create(Class<T> type) {  
        T result = super.create(type);  
        if(type.equals(Mail.class)){  
            ((Mail)result).setUseFor("曾濤平");
        }  
        return result;  
    }  

}
           

在這個工廠類中,我們對Mail類中無參構造函數構造出來的對象做了個特殊處理,加了個我的名字。接下來,我們在配置檔案中配置這個類為我們想要的對象工廠類。

  寫好了ObjectFactory, 僅需做如下配置: 

<objectFactory type="Factory.ExampleObjectFactory">
        <property name="someProperty" value="100"/>
     </objectFactory>
           

然後我們執行測試代碼,查詢一條使用者記錄出來,發現傳回的對象中的确設定了我的名字,如下所示:

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
SQL:[select id, create_time, modify_time, web_id, mail, use_for from mail where id = 67;]執行耗時[5644ms]
Mail [id=67, createTime=Wed Jan 17 14:39:04 CST 2018, modifyTime=Wed Jan 17 14:39:04 CST 2018, webId=2, mail=Address [province=Address [province=null, city= city=7]], useFor=曾濤平]
           

至此,關于mybatis中的對象工廠相關的知識點便講解的差不多了。