MyBatis是如何實現日誌模組的詳解
場景復現
你知道MyBatis是怎麼實現日誌的?額,這個簡單,我知道啊!不就是在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>
看,就是上面這麼配置就行了,這樣還可以實現日誌切換呢!
看官方文件介紹,有如下日誌實現可以切換:
你知道具體的原理嗎?MyBatis是怎樣實現日誌切換的呢?
額,這個,這個...(撓頭,原始碼還沒看過呢,😓)
原始碼擼起來~
對於這個不確定的事,作為程式設計師的我,怎能輕易說不知道呢?!原始碼走起來~
找到一個入口
首先,先從GitHub上將MyBatis的原始碼給clone到本地.老大們擼了那麼多行程式碼,從哪開始下手呢???
大佬們,也是很規範的,寫完程式碼有不少的Junit測試類,從test/java中找到了logging包.那就從這開始吧~
@Test public void shouldReadLogImplFromSettings() throws Exception { //try-with-resources語句,目的就是為了讀取配置檔案IO //mybatis-config.xml配置檔案內容如上,<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類
//根據配置檔案IO構建SqlSessionFactory public SqlSessionFactory build(Reader reader) { return build(reader,null,null); } public SqlSessionFactory build(Reader reader,String environment,Properties properties) { try { //構建XML配置構建器 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配置構建器
建構函式
//reader是XML配置檔案IO public XMLConfigBuilder(Reader reader,Properties props) { this(new XPathParser(reader,true,props,new XMLMapperEntityResolver()),props); } //實際呼叫的XML配置檔案構建器構建函式 private XMLConfigBuilder(XPathParser parser,Properties props) { //注意了,這是重點,在這地方呼叫了父類的建構函式,新建了一個Configuration物件,下面看看Configuration建構函式做了什麼 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; //根據XPATH獲取configuration節點 parseConfiguration(parser.evalNode("/configuration")); return configuration; }
parseConfiguration 解析配置方法
private void parseConfiguration(XNode root) { try { //issue #117 read properties first // 解析 properties 節點 propertiesElement(root.evalNode("properties")); // 解析 settings 節點 Properties settings = settingsAsProperties(root.evalNode("settings")); loadCustomVfs(settings); // 解析 typeAliases 節點 typeAliasesElement(root.evalNode("typeAliases")); // 解析 plugins 節點 pluginElement(root.evalNode("plugins")); // 解析 objectFactory 節點 objectFactoryElement(root.evalNode("objectFactory")); // 解析 objectWrapperFactory 節點 objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); // 解析 reflectorFactory 節點 reflectorFactoryElement(root.evalNode("reflectorFactory")); settingsElement(settings); // 解析 environments 節點, 需要在 objectFactory 和 objectWrapperFactory才能讀取 environmentsElement(root.evalNode("environments")); // 解析 databaseIdProvider 節點 databaseIdProviderElement(root.evalNode("databaseIdProvider")); // 解析 typeHandlers 節點 typeHandlerElement(root.evalNode("typeHandlers")); // 解析 mappers 節點 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節點,將settings節點的內容解析到 Properties 物件,其中涉及到很多操作,不在本文分析之列.
settingsElement(settings);將settings中的配置設定到Configuration物件.
settingsElement settings節點的配置
private void settingsElement(Properties props) throws Exception { ... configuration.setLogPrefix(props.getProperty("logPrefix")); @SuppressWarnings("unchecked") // 這個不就是從settings中解析日誌的實現嗎?快看看~ // resovleClass是父類BaseBuilder中的方法 Class<? extends Log> logImpl = (Class<? extends Log>) resolveClass(props.getProperty("logImpl")); //將日誌的實現類設定到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建構函式中可以看到,類型別名中定義了日誌實現的名稱與,實現類.class(這些實現類都是MyBatis實現的,稍後再說)
//設定日誌實現類 public void setLogImpl(Class<? extends Log> logImpl) { if (logImpl != null) { this.logImpl = logImpl; //呼叫日誌工廠,設定日誌實現 LogFactory.useCustomLogging(this.logImpl); } }
BaseBuilder
建構函式
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); } } //這個不就是從別名中獲取對應的實現嗎??! //配置SLF4J,返回Slf4jImpl.class protected <T> Class<? extends T> resolveAlias(String alias) { //這個地方的typeAliasRegistry是從configuration中獲取的 return typeAliasRegistry.resolveAlias(alias); }
TypeAliasRegistry 類型別名
類型別名註冊
//就是將key轉換位小寫,存入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); }
//根據對應的別名key,獲取實現類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實現類
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,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); } ... }
可見,最終還是呼叫具體的日誌API實現!
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"; // 記錄當前使用的第三方日誌庫元件所對應的介面卡的方法 private static Constructor<? extends Log> logConstructor; // tryImplementation 進行嘗試載入 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 的方法都是嘗試載入日誌的實現 * 最終的實現都是 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 { // 會呼叫 useSlf4jLogging 類似的方法 runnable.run(); } catch (Throwable t) { // ignore } } } /** * 設計日誌的實現類 * @param implClass Log 的子類 */ 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); } } }
可以看到其中有靜態程式碼塊,隨著類的載入,嘗試載入日誌的實現!
總結
通過以上的一步一步分析,可以看到MyBatis是分別通過對日誌實現進行進行包裝,最終還是呼叫具體的日誌API實現.
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支援。