重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

MyBatis中怎么实现一个日志模块

这期内容当中小编将会给大家带来有关MyBatis中怎么实现一个日志模块,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都网站制作、科尔沁网络推广、微信小程序、科尔沁网络营销、科尔沁企业策划、科尔沁品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供科尔沁建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

SqlSessionFactoryBuilder类

//根据配置文件IO构建SqlSessionFactorypublic 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配置文件IOpublic XMLConfigBuilder(Reader reader, String environment, Properties props) {  this(new XPathParser(reader, true, props, new XMLMapperEntityResolver()), environment, props);}//实际调用的XML配置文件构建器构建函数private XMLConfigBuilder(XPathParser parser, String environment, 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 logImpl = (Class) 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 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 Class 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.classprotected Class 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,获取实现类classpublic Class resolveAlias(String string) {  try {    if (string == null) {      return null;    }    // issue #748    String key = string.toLowerCase(Locale.ENGLISH);    Class value;    if (TYPE_ALIASES.containsKey(key)) {      value = (Class) TYPE_ALIASES.get(key);    } else {      value = (Class) 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, 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); } ...}

可见,最终还是调用具体的日志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 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 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 implClass) {  try {   // 通过反射获取构造方法   Constructor 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中怎么实现一个日志模块了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网站栏目:MyBatis中怎么实现一个日志模块
网站地址:http://cqcxhl.com/article/gchchh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP