MyBatis是怎么實(shí)現(xiàn)日志模塊的?

場景復(fù)現(xiàn)

你知道MyBatis是怎么實(shí)現(xiàn)日志的?額,這個簡單,我知道啊!不就是在mybatis-config.xml文件中配置一下嗎?

<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="SLF4J"/>
    </settings>
</configuration>

看,就是上面這么配置就行了,這樣還可以實(shí)現(xiàn)日志切換呢!
看官方文檔介紹,有如下日志實(shí)現(xiàn)可以切換:

mybatis支持的日志實(shí)現(xiàn)

你知道具體的原理嗎?MyBatis是怎樣實(shí)現(xiàn)日志切換的呢?
額,這個,這個...(撓頭,源碼還沒看過呢,??)

源碼擼起來~

對于這個不確定的事,作為程序員的我,怎能輕易說不知道呢?!源碼走起來~

找到一個入口

首先,先從GitHub上將MyBatis的源碼給clone到本地.老大們擼了那么多行代碼,從哪開始下手呢???
大佬們,也是很規(guī)范的,寫完代碼有不少的Junit測試類,從test/java中找到了logging包.那就從這開始吧~

logging測試類

@Test
public void shouldReadLogImplFromSettings() throws Exception {
//try-with-resources語句,目的就是為了讀取配置文件IO
//mybatis-config.xml配置文件內(nèi)容如上,<setting name="logImpl" value="SLF4J"/>
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/logging/mybatis-config.xml")) {
  //解析配置文件,解析配置文件的代碼就在這了~
  new SqlSessionFactoryBuilder().build(reader);
}

Log log = LogFactory.getLog(Object.class);
log.debug("Debug message.");
assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
}

SqlSessionFactoryBuilder類

//根據(jù)配置文件IO構(gòu)建SqlSessionFactory
public SqlSessionFactory build(Reader reader) {
    return build(reader, null, null);
}


public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
    try {
        //構(gòu)建XML配置構(gòu)建器
        XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
        //解析XML配置文件
        return build(parser.parse());
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
        ErrorContext.instance().reset();
        try {
            reader.close();
        } catch (IOException e) {
            // Intentionally ignore. Prefer previous error.
        }
    }
}

XMLConfigBuilder XML配置構(gòu)建器

構(gòu)造函數(shù)
//reader是XML配置文件IO
public XMLConfigBuilder(Reader reader, String environment, Properties props) {
    this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);
}

//實(shí)際調(diào)用的XML配置文件構(gòu)建器構(gòu)建函數(shù)
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
    //注意了,這是重點(diǎn),在這地方調(diào)用了父類的構(gòu)造函數(shù),新建了一個Configuration對象,下面看看Configuration構(gòu)造函數(shù)做了什么
    super(new Configuration());
    ErrorContext.instance().resource("SQL Mapper Configuration");
    this.configuration.setVariables(props);
    this.parsed = false;
    this.environment = environment;
    this.parser = parser;
}
parse解析方法
/**
 * 解析 mybatis-config.xml
 * @return
 */
public Configuration parse() {
    // 只能解析一次
    if (parsed) {
        throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    //根據(jù)XPATH獲取configuration節(jié)點(diǎn)
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
}
parseConfiguration 解析配置方法
private void parseConfiguration(XNode root) {
    try {
        //issue #117 read properties first
        // 解析 properties 節(jié)點(diǎn)
        propertiesElement(root.evalNode("properties"));
        // 解析 settings 節(jié)點(diǎn)
        Properties settings = settingsAsProperties(root.evalNode("settings"));
        loadCustomVfs(settings);
        // 解析 typeAliases 節(jié)點(diǎn)
        typeAliasesElement(root.evalNode("typeAliases"));
        // 解析 plugins 節(jié)點(diǎn)
        pluginElement(root.evalNode("plugins"));
        // 解析 objectFactory 節(jié)點(diǎn)
        objectFactoryElement(root.evalNode("objectFactory"));
        // 解析 objectWrapperFactory 節(jié)點(diǎn)
        objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
        // 解析 reflectorFactory 節(jié)點(diǎn)
        reflectorFactoryElement(root.evalNode("reflectorFactory"));
        settingsElement(settings);
        // 解析 environments 節(jié)點(diǎn)蚀同, 需要在 objectFactory 和 objectWrapperFactory才能讀取
        environmentsElement(root.evalNode("environments"));
        // 解析 databaseIdProvider 節(jié)點(diǎn)
        databaseIdProviderElement(root.evalNode("databaseIdProvider"));
        // 解析 typeHandlers 節(jié)點(diǎn)
        typeHandlerElement(root.evalNode("typeHandlers"));
        // 解析 mappers 節(jié)點(diǎn)
        mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
        throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
}

其中Properties settings = settingsAsProperties(root.evalNode("settings"));是解析setttings節(jié)點(diǎn),將settings節(jié)點(diǎn)的內(nèi)容解析到 Properties 對象,其中涉及到很多操作,不在本文分析之列.
settingsElement(settings);將settings中的配置設(shè)置到Configuration對象.

settingsElement settings節(jié)點(diǎn)的配置
private void settingsElement(Properties props) throws Exception {
    ...
    configuration.setLogPrefix(props.getProperty("logPrefix"));
    @SuppressWarnings("unchecked")
    // 這個不就是從settings中解析日志的實(shí)現(xiàn)嗎?快看看~
    // resovleClass是父類BaseBuilder中的方法
    Class<? extends Log> logImpl = (Class<? extends Log>) resolveClass(props.getProperty("logImpl"));
    //將日志的實(shí)現(xiàn)類設(shè)置到configuration配置類中
    configuration.setLogImpl(logImpl);
    configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
}

Configuration 配置類

public class Configuration {
    ...
    protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
    ...
    public Configuration() {
        ...
    
        //日志類型別名
        typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
        typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
        typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
        typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
        typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
        typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
        typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);
    
        typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
        typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);
    
        languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
        languageRegistry.register(RawLanguageDriver.class);
    }
}

從上面的Configuration構(gòu)造函數(shù)中可以看到,類型別名中定義了日志實(shí)現(xiàn)的名稱與,實(shí)現(xiàn)類.class(這些實(shí)現(xiàn)類都是MyBatis實(shí)現(xiàn)的,稍后再說)

//設(shè)置日志實(shí)現(xiàn)類
public void setLogImpl(Class<? extends Log> logImpl) {
    if (logImpl != null) {
        this.logImpl = logImpl;
        //調(diào)用日志工廠,設(shè)置日志實(shí)現(xiàn)
        LogFactory.useCustomLogging(this.logImpl);
    }
}

BaseBuilder

構(gòu)造函數(shù)
public BaseBuilder(Configuration configuration) {
    this.configuration = configuration;
    //可以看到這個地方的類型別名是從configuration中獲取的
    this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();
    this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
}
protected <T> Class<? extends T> resolveClass(String alias) {
    if (alias == null) {
      return null;
    }
    try {
      //解析別名
      return resolveAlias(alias);
    } catch (Exception e) {
      throw new BuilderException("Error resolving class. Cause: " + e, e);
    }
}

//這個不就是從別名中獲取對應(yīng)的實(shí)現(xiàn)嗎??!
//配置SLF4J,返回Slf4jImpl.class
protected <T> Class<? extends T> resolveAlias(String alias) {
    //這個地方的typeAliasRegistry是從configuration中獲取的
    return typeAliasRegistry.resolveAlias(alias);
}

TypeAliasRegistry 類型別名

類型別名注冊
//就是將key轉(zhuǎn)換位小寫,存入HashMap中,key是別名,value是Class類對象
public void registerAlias(String alias, Class<?> value) {
    if (alias == null) {
        throw new TypeException("The parameter alias cannot be null");
    }
    // issue #748
    String key = alias.toLowerCase(Locale.ENGLISH);
    if (TYPE_ALIASES.containsKey(key) && TYPE_ALIASES.get(key) != null && !TYPE_ALIASES.get(key).equals(value)) {
        throw new TypeException("The alias '" + alias + "' is already mapped to the value '" + TYPE_ALIASES.get(key).getName() + "'.");
    }
    TYPE_ALIASES.put(key, value);
}
//根據(jù)對應(yīng)的別名key,獲取實(shí)現(xiàn)類class
public <T> Class<T> resolveAlias(String string) {
    try {
        if (string == null) {
            return null;
        }
        // issue #748
        String key = string.toLowerCase(Locale.ENGLISH);
        Class<T> value;
        if (TYPE_ALIASES.containsKey(key)) {
            value = (Class<T>) TYPE_ALIASES.get(key);
        } else {
            value = (Class<T>) Resources.classForName(string);
        }
        return value;
    } catch (ClassNotFoundException e) {
        throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);
    }
}

Slf4jImpl Slf4j實(shí)現(xiàn)類

public class Slf4jImpl implements Log {

  private Log log;

  public Slf4jImpl(String clazz) {
    //使用Slf4j的API獲取日志器
    Logger logger = LoggerFactory.getLogger(clazz);

    if (logger instanceof LocationAwareLogger) {
      try {
        // check for slf4j >= 1.6 method signature
        logger.getClass().getMethod("log", Marker.class, String.class, int.class, String.class, Object[].class, Throwable.class);
        log = new Slf4jLocationAwareLoggerImpl((LocationAwareLogger) logger);
        return;
      } catch (SecurityException e) {
        // fail-back to Slf4jLoggerImpl
      } catch (NoSuchMethodException e) {
        // fail-back to Slf4jLoggerImpl
      }
    }

    // Logger is not LocationAwareLogger or slf4j version < 1.6
    log = new Slf4jLoggerImpl(logger);
  }

  ...
}

可見,最終還是調(diào)用具體的日志API實(shí)現(xiàn)!

LogFactory 日志工廠

package org.apache.ibatis.logging;

import java.lang.reflect.Constructor;

/**
 * 日志工廠
 */
public final class LogFactory {

  /**
   * Marker to be used by logging implementations that support markers
   */
  public static final String MARKER = "MYBATIS";

  // 記錄當(dāng)前使用的第三方日志庫組件所對應(yīng)的適配器的方法
  private static Constructor<? extends Log> logConstructor;

  // tryImplementation 進(jìn)行嘗試加載
  static {
    tryImplementation(LogFactory::useSlf4jLogging);
    tryImplementation(LogFactory::useCommonsLogging);
    tryImplementation(LogFactory::useLog4J2Logging);
    tryImplementation(LogFactory::useLog4JLogging);
    tryImplementation(LogFactory::useJdkLogging);
    tryImplementation(LogFactory::useNoLogging);
  }

  // 私有化
  private LogFactory() {
    // disable construction
  }

  public static Log getLog(Class<?> aClass) {
    return getLog(aClass.getName());
  }

  public static Log getLog(String logger) {
    try {
      return logConstructor.newInstance(logger);
    } catch (Throwable t) {
      throw new LogException("Error creating logger for logger " + logger + ".  Cause: " + t, t);
    }
  }

  /**
   * 以下的 useXXXogging 的方法都是嘗試加載日志的實(shí)現(xiàn)
   * 最終的實(shí)現(xiàn)都是 setImplementation
   */


  public static synchronized void useCustomLogging(Class<? extends Log> clazz) {
    setImplementation(clazz);
  }

  public static synchronized void useSlf4jLogging() {
    setImplementation(org.apache.ibatis.logging.slf4j.Slf4jImpl.class);
  }

  public static synchronized void useCommonsLogging() {
    setImplementation(org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl.class);
  }

  public static synchronized void useLog4JLogging() {
    setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
  }

  public static synchronized void useLog4J2Logging() {
    setImplementation(org.apache.ibatis.logging.log4j2.Log4j2Impl.class);
  }

  public static synchronized void useJdkLogging() {
    setImplementation(org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl.class);
  }

  public static synchronized void useStdOutLogging() {
    setImplementation(org.apache.ibatis.logging.stdout.StdOutImpl.class);
  }

  public static synchronized void useNoLogging() {
    setImplementation(org.apache.ibatis.logging.nologging.NoLoggingImpl.class);
  }

  /**
   * 嘗試加載
   * @param runnable
   */
  private static void tryImplementation(Runnable runnable) {
    if (logConstructor == null) {
      try {
        // 會調(diào)用 useSlf4jLogging 類似的方法
        runnable.run();
      } catch (Throwable t) {
        // ignore
      }
    }
  }

  /**
   * 設(shè)計日志的實(shí)現(xiàn)類
   * @param implClass Log 的子類
   */
  private static void setImplementation(Class<? extends Log> implClass) {
    try {
      // 通過反射獲取構(gòu)造方法
      Constructor<? extends Log> candidate = implClass.getConstructor(String.class);
      Log log = candidate.newInstance(LogFactory.class.getName());
      if (log.isDebugEnabled()) {
        log.debug("Logging initialized using '" + implClass + "' adapter.");
      }
      //設(shè)置日志實(shí)現(xiàn)的有參構(gòu)造函數(shù)
      logConstructor = candidate;
    } catch (Throwable t) {
      throw new LogException("Error setting Log implementation.  Cause: " + t, t);
    }
  }

}

可以看到其中有靜態(tài)代碼塊,隨著類的加載,嘗試加載日志的實(shí)現(xiàn)!

總結(jié)

通過以上的一步一步分析,可以看到MyBatis是分別通過對日志實(shí)現(xiàn)進(jìn)行包裝,最終還是調(diào)用具體的日志API實(shí)現(xiàn).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市啊掏,隨后出現(xiàn)的幾起案子蠢络,更是在濱河造成了極大的恐慌,老刑警劉巖迟蜜,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件刹孔,死亡現(xiàn)場離奇詭異,居然都是意外死亡娜睛,警方通過查閱死者的電腦和手機(jī)髓霞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來畦戒,“玉大人方库,你說我怎么就攤上這事≌险” “怎么了纵潦?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長垃环。 經(jīng)常有香客問我邀层,道長,這世上最難降的妖魔是什么晴裹? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任被济,我火速辦了婚禮,結(jié)果婚禮上涧团,老公的妹妹穿的比我還像新娘只磷。我一直安慰自己经磅,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布钮追。 她就那樣靜靜地躺著预厌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪元媚。 梳的紋絲不亂的頭發(fā)上轧叽,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天,我揣著相機(jī)與錄音刊棕,去河邊找鬼炭晒。 笑死,一個胖子當(dāng)著我的面吹牛甥角,可吹牛的內(nèi)容都是我干的网严。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼嗤无,長吁一口氣:“原來是場噩夢啊……” “哼震束!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起当犯,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤垢村,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嚎卫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘉栓,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年驰凛,在試婚紗的時候發(fā)現(xiàn)自己被綠了胸懈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡恰响,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出涌献,到底是詐尸還是另有隱情胚宦,我是刑警寧澤,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布燕垃,位于F島的核電站枢劝,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏卜壕。R本人自食惡果不足惜您旁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望轴捎。 院中可真熱鬧鹤盒,春花似錦蚕脏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至尺碰,卻和暖如春挣棕,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亲桥。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工洛心, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人题篷。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓皂甘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親悼凑。 傳聞我的和親對象是個殘疾皇子偿枕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

推薦閱讀更多精彩內(nèi)容