欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis日志模塊的適配器模式詳解

 更新時(shí)間:2022年08月09日 14:56:51   作者:周杰倫本人???????  
這篇文章主要介紹了Mybatis日志模塊的適配器模式詳解,,mybatis用了適配器模式來(lái)兼容這些框架,適配器模式就是通過(guò)組合的方式,將需要適配的類(lèi)轉(zhuǎn)為使用者能夠使用的接口

Mybatis的日志模塊的適配器模式

我們?cè)陂_(kāi)發(fā)中日志是必不可少的一部分,而市場(chǎng)中有很多日志框架供我們使用,mybatis作為一個(gè)開(kāi)源框架需要兼容這些框架,mybatis用了適配器模式來(lái)兼容這些框架,適配器模式就是通過(guò)組合的方式,將需要適配的類(lèi)轉(zhuǎn)為使用者能夠使用的接口

下面咱們分析一下mybatis的日志模塊

mybatis定義了自己的Log接口

Log接口

public interface Log {
  boolean isDebugEnabled();
  boolean isTraceEnabled();
  void error(String s, Throwable e);
  void error(String s);
  void debug(String s);
  void trace(String s);
  void warn(String s);
}

這里定義了一些常用的方法

LogFactory的靜態(tài)代碼塊加載對(duì)應(yīng)是日志適配器:

日志工廠類(lèi)LogFactory

    static {
        tryImplementation(LogFactory::useSlf4jLogging);
        tryImplementation(LogFactory::useCommonsLogging);
        tryImplementation(LogFactory::useLog4J2Logging);
        tryImplementation(LogFactory::useLog4JLogging);
        tryImplementation(LogFactory::useJdkLogging);
        tryImplementation(LogFactory::useNoLogging);
    }
    private static void tryImplementation(Runnable runnable) {
        if (logConstructor == null) {
            try {
                runnable.run();
            } catch (Throwable t) {
                // ignore
            }
        }
    }
    public static synchronized void useLog4JLogging() {
        setImplementation(org.apache.ibatis.logging.log4j.Log4jImpl.class);
    }

當(dāng)logConstructor用來(lái)記錄日志適配器的構(gòu)造方法,當(dāng)logConstructor不為空的時(shí)候就不再加載其他的日志適配器了。

分析一下setImplementation()方法:

private static void setImplementation(Class<? extends Log> implClass) {
        try {
            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.");
            }
            logConstructor = candidate;
        } catch (Throwable t) {
            throw new LogException("Error setting Log implementation.  Cause: " + t, t);
        }
    }
  • 獲取適配器的構(gòu)造方法
  • 然后獲取這個(gè)適配器的實(shí)例,如果獲取成功,把它的構(gòu)造方法記錄在logConstructor中,否則就會(huì)拋出異常

下面就分析一下Log4jImpl類(lèi):

Log接口的實(shí)現(xiàn)類(lèi)

public class Log4jImpl implements Log {
  private static final String FQCN = Log4jImpl.class.getName();
  private final Logger log;
  public Log4jImpl(String clazz) {
    log = Logger.getLogger(clazz);
  }

  @Override
  public boolean isDebugEnabled() {
    return log.isDebugEnabled();
  }

  @Override
  public boolean isTraceEnabled() {
    return log.isTraceEnabled();
  }

  @Override
  public void error(String s, Throwable e) {
    log.log(FQCN, Level.ERROR, s, e);
  }

  @Override
  public void error(String s) {
    log.log(FQCN, Level.ERROR, s, null);
  }

  @Override
  public void debug(String s) {
    log.log(FQCN, Level.DEBUG, s, null);
  }

  @Override
  public void trace(String s) {
    log.log(FQCN, Level.TRACE, s, null);
  }

  @Override
  public void warn(String s) {
    log.log(FQCN, Level.WARN, s, null);
  }

}

Log4jImpl是一個(gè)適配器類(lèi),它實(shí)現(xiàn)了Mybatis自定義的Log接口,它的成員變量Logger是log4j里的類(lèi),我們要做的是調(diào)用Log接口的方法就可以使用log4j,Logger是需要我們適配的類(lèi),因此我們?cè)趯?shí)現(xiàn)Log接口的方法的時(shí)候調(diào)用Logger中的方法來(lái)完成適配,這就是適配器模式的實(shí)現(xiàn)

總結(jié)

Mybatis日志模塊用到了適配器模式,Log接口是目標(biāo)接口,Logger等各種日志框架的類(lèi)是需要我們適配的類(lèi),而是適配器是Log4jImpl、Slf4jImpl等適配類(lèi)

到此這篇關(guān)于Mybatis日志模塊的適配器模式詳解的文章就介紹到這了,更多相關(guān)Mybatis適配器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論