Mybatis源码之配置初始化

基础示例

一个Mybatis的项目基本配置信息是写在一个xml文件中,指定数据库类型、数据源、事务等相关信息,如下




  
    
      
      
        
        
        
        
      
    
  
  
    
  

程序执行之初会解析配置文件,后续将根据配置的信息初始化数据连接池、Mapper等,下面根据源码看看配置文件的解析。

下面代码展示了根据配置文件读取文件流,解析文件流并生成 configuration 并生成 SqlSessionFactory

// 配置文件的位置
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
// SqlSessionFactoryBuilder 会根据 文件流信息解析成configuration
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

构建 SqlSessionFactory

public SqlSessionFactory build(InputStream inputStream) {
    return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
    return build(parser.parse());
}

新建 XMLConfigBuilder,XMLConfigBuilder将负责将 xml 配置文件解析成 Configuration 实例,供其他实例使用。下面重点介绍Configuration 实例解析过程。

创建XMLConfigBuilder

public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {
  this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);
}

private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {
  super(new Configuration());
  ErrorContext.instance().resource("SQL Mapper Configuration");
  this.configuration.setVariables(props);
  this.parsed = false;
  this.environment = environment;
  this.parser = parser;
}

调用构造函数时会新建一个 XPathParser,parser会解析配置文件的信息成 document;同时调用了父类的构造函数,传入新建的一个Configuration,这个新建的 Configuration 就是 parse() 方法的返回值,后续对配置文件的解析也是将配置文件的数据放到此 configuration 中。

// XPathParser 的构造函数
public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
  commonConstructor(validation, variables, entityResolver);
  this.document = createDocument(new InputSource(inputStream));
}

XMLConfigBuilder初始化完成,调用 parse() 方法解析配置文件的 configuration 节点,这是整个配置文件的解析,也就是官方XML配置说明的实现部分。

public Configuration parse() {
  if (parsed) {
    throw new BuilderException("Each XMLConfigBuilder can only be used once.");
  }
  parsed = true;
  // 拿到configuration节点,调用parseConfiguration方法,将解析结果放在configuration变量中
  parseConfiguration(parser.evalNode("/configuration"));
  return configuration;
}

private void parseConfiguration(XNode root) {
  try {
    //issue #117 read properties first
    propertiesElement(root.evalNode("properties"));
    Properties settings = settingsAsProperties(root.evalNode("settings"));
    loadCustomVfs(settings);
    typeAliasesElement(root.evalNode("typeAliases"));
    pluginElement(root.evalNode("plugins"));
    objectFactoryElement(root.evalNode("objectFactory"));
    objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
    reflectorFactoryElement(root.evalNode("reflectorFactory"));
    settingsElement(settings);
    // read it after objectFactory and objectWrapperFactory issue #631
    environmentsElement(root.evalNode("environments"));
    databaseIdProviderElement(root.evalNode("databaseIdProvider"));
    typeHandlerElement(root.evalNode("typeHandlers"));
    mapperElement(root.evalNode("mappers"));
  } catch (Exception e) {
    throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
  }
}

propertiesElement(root.evalNode("properties"));

properties节点示例,一般数据库的连接配置会单独拿出来,放在外部配置中,resource 或者 url属性指定


  
  

private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
        // 读取 properties 节点的子节点,加载为 Properties
      Properties defaults = context.getChildrenAsProperties();
        // resource 和 url都是外部配置资源路径
      String resource = context.getStringAttribute("resource");
      String url = context.getStringAttribute("url");
        // 指定其中之一配置
      if (resource != null && url != null) {
        throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
      }
        // 加载外部配置
      if (resource != null) {
        defaults.putAll(Resources.getResourceAsProperties(resource));
      } else if (url != null) {
        defaults.putAll(Resources.getUrlAsProperties(url));
      }
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }
        // 解析完毕,设置值
      parser.setVariables(defaults);
      configuration.setVariables(defaults);
    }
}
  1. 属性放在configuration的variables中
  2. XPathParser 的 variables中,解析其他数据需要使用

settingsAsProperties(root.evalNode("settings"));

settings 节点,这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。实例 如下


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

settingsAsProperties 方法代码

private Properties settingsAsProperties(XNode context) {
  if (context == null) {
    return new Properties();
  }
  Properties props = context.getChildrenAsProperties();
  // 检查属性是否在Configuration类中存在
  // Check that all settings are known to the configuration class
  MetaClass metaConfig = MetaClass.forClass(Configuration.class, localReflectorFactory);
  for (Object key : props.keySet()) {
    if (!metaConfig.hasSetter(String.valueOf(key))) {
      throw new BuilderException("The setting " + key + " is not known.  Make sure you spelled it correctly (case sensitive).");
    }
  }
  return props;
}

获取属性值,并返回,返回的数据在 settingsElement(settings); 方法中设置到configuration中

loadCustomVfs(settings);

指定VFS的实现,自定义VFS的实现的类全限定名,以逗号分隔。(不知道是啥)

private void loadCustomVfs(Properties props) throws ClassNotFoundException {
  String value = props.getProperty("vfsImpl");
  if (value != null) {
    String[] clazzes = value.split(",");
    for (String clazz : clazzes) {
      if (!clazz.isEmpty()) {
        @SuppressWarnings("unchecked")
        Class vfsImpl = (Class)Resources.classForName(clazz);
        configuration.setVfsImpl(vfsImpl);
      }
    }
  }
}

需要是 VFS 的子类,VFS类的注释

提供一个非常简单的API,用于访问应用程序服务器中的资源。

应该是为了某种扩展吧。

typeAliasesElement(root.evalNode("typeAliases"));

类型别名是为 Java 类型设置一个短的名字。它只和 XML 配置有关,存在的意义仅在于用来减少类完全限定名的冗余。


  
  
  
  

typeAliasesElement 代码如下

private void typeAliasesElement(XNode parent) {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      if ("package".equals(child.getName())) {
        String typeAliasPackage = child.getStringAttribute("name");
        configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
      } else {
        String alias = child.getStringAttribute("alias");
        String type = child.getStringAttribute("type");
        try {
          Class clazz = Resources.classForName(type);
          if (alias == null) {
              // 这个 typeAliasRegistry 就是 configuration 的 typeAliasRegistry属性,在 BaseBuilder的构造函数中赋值
            typeAliasRegistry.registerAlias(clazz);
          } else {
            typeAliasRegistry.registerAlias(alias, clazz);
          }
        } catch (ClassNotFoundException e) {
          throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
        }
      }
    }
  }
}

几个相关方法

public void registerAliases(String packageName){
    registerAliases(packageName, Object.class);
}

public void registerAliases(String packageName, Class superType){
    ResolverUtil> resolverUtil = new ResolverUtil>();
    resolverUtil.find(new ResolverUtil.IsA(superType), packageName);
    Set>> typeSet = resolverUtil.getClasses();
    for(Class type : typeSet){
        // Ignore inner classes and interfaces (including package-info.java)
        // Skip also inner classes. See issue #6
        if (!type.isAnonymousClass() && !type.isInterface() && !type.isMemberClass()) {
            registerAlias(type);
        }
    }
}

public void registerAlias(Class type) {
    // 类名
    String alias = type.getSimpleName();
    Alias aliasAnnotation = type.getAnnotation(Alias.class);
    if (aliasAnnotation != null) {
        alias = aliasAnnotation.value();
    } 
    registerAlias(alias, type);
}

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);
}

  1. 设置包名的将会扫描包下的类,设置别名
  2. 没设置 alias 会以类名为别名
  3. 别名全小写
  4. TypeAliasRegistry 类的构造函数已经设置了一些基础数据类型以及集合数组类型等

将短名字添加到configuration的 typeAliasRegistry 属性中

pluginElement(root.evalNode("plugins"));

MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。

private void pluginElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      String interceptor = child.getStringAttribute("interceptor");
      Properties properties = child.getChildrenAsProperties();
        // 实例化
      Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance();
        // 设置 properties
      interceptorInstance.setProperties(properties);
        // 添加到 interceptor 链中,@see InterceptorChain
      configuration.addInterceptor(interceptorInstance);
    }
  }
}

objectFactoryElement(root.evalNode("objectFactory"));

MyBatis 每次创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成。 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认构造方法,要么在参数映射存在的时候通过参数构造方法来实例化。 如果想覆盖对象工厂的默认行为,则可以通过创建自己的对象工厂来实现。 默认声明的是ObjectFactory objectFactory = new DefaultObjectFactory(); 。

private void objectFactoryElement(XNode context) throws Exception {
  if (context != null) {
    String type = context.getStringAttribute("type");
    Properties properties = context.getChildrenAsProperties();
    ObjectFactory factory = (ObjectFactory) resolveClass(type).newInstance();
    factory.setProperties(properties);
    configuration.setObjectFactory(factory);
  }
}

objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));

reflectorFactoryElement(root.evalNode("reflectorFactory"));

settingsElement(settings);

将 settings 节点配置的参数设置到 configuration 中

environmentsElement(root.evalNode("environments"));

MyBatis 可以配置成适应多种环境,这种机制有助于将 SQL 映射应用于多种数据库之中, 现实情况下有多种理由需要这么做。例如,开发、测试和生产环境需要有不同的配置;或者共享相同 Schema 的多个生产数据库, 想使用相同的 SQL 映射。许多类似的用例。

每个数据库对应一个 SqlSessionFactory 实例


  
    
      
    
    
      
      
      
      
    
  

注意这里的关键点:

  • 默认的环境 ID(比如:default="development")。在构建session工厂的时候指定,如未指定则为默认值:SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment);
  • 每个 environment 元素定义的环境 ID(比如:id="development")。
  • 事务管理器的配置(比如:type="JDBC")。
  • 数据源的配置(比如:type="POOLED")。
private void environmentsElement(XNode context) throws Exception {
  if (context != null) {
    if (environment == null) {
      environment = context.getStringAttribute("default");
    }
    for (XNode child : context.getChildren()) {
      String id = child.getStringAttribute("id");
      if (isSpecifiedEnvironment(id)) {
          // 事务管理器
        TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
          // 数据源
        DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
        DataSource dataSource = dsFactory.getDataSource();
          // builder 模式构建 environment
        Environment.Builder environmentBuilder = new Environment.Builder(id)
            .transactionFactory(txFactory)
            .dataSource(dataSource);
        configuration.setEnvironment(environmentBuilder.build());
      }
    }
  }
}

内置的数据源和事务管理器相关的类,在 Configuration 构造函数中,添加到 typeAliasRegistry 中

  1. 事务管理器
  2. 数据源
  3. 根据事务管理器、数据源、id生成Environment实例,赋值到configuration的environment

databaseIdProviderElement(root.evalNode("databaseIdProvider"));

MyBatis 可以根据不同的数据库厂商执行不同的语句,这种多厂商的支持是基于映射语句中的 databaseId 属性。 MyBatis 会加载不带 databaseId 属性和带有匹配当前数据库 databaseId 属性的所有语句。 如果同时找到带有 databaseId 和不带 databaseId 的相同语句,则后者会被舍弃。 为支持多厂商特性只要像下面这样在 mybatis-config.xml 文件中加入 databaseIdProvider 即可:


typeHandlerElement(root.evalNode("typeHandlers"));

无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值时, 都会用类型处理器将获取的值以合适的方式转换成 Java 类型。 你可以重写类型处理器或创建你自己的类型处理器来处理不支持的或非标准的类型。 具体做法为:实现 org.apache.ibatis.type.TypeHandler 接口, 或继承一个很便利的类 org.apache.ibatis.type.BaseTypeHandler, 然后可以选择性地将它映射到一个 JDBC 类型。

配置文件声明自定义handler


  
 
private void typeHandlerElement(XNode parent) throws Exception {
  if (parent != null) {
    for (XNode child : parent.getChildren()) {
      if ("package".equals(child.getName())) {
        String typeHandlerPackage = child.getStringAttribute("name");
        typeHandlerRegistry.register(typeHandlerPackage);
      } else {
        String javaTypeName = child.getStringAttribute("javaType");
        String jdbcTypeName = child.getStringAttribute("jdbcType");
        String handlerTypeName = child.getStringAttribute("handler");
        Class javaTypeClass = resolveClass(javaTypeName);
        JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
        Class typeHandlerClass = resolveClass(handlerTypeName);
          // 也就是说配置不指定javaType只指定jdbcType是无效的
        if (javaTypeClass != null) {
          if (jdbcType == null) {
            typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
          } else {
            typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
          }
        } else {
          typeHandlerRegistry.register(typeHandlerClass);
        }
      }
    }
  }
}
// register 最后都会通过调用TypeHandlerRegistry 的 下面方法执行 注册
private void register(Type javaType, JdbcType jdbcType, TypeHandler handler) {
    if (javaType != null) {
        Map> map = TYPE_HANDLER_MAP.get(javaType);
        if (map == null || map == NULL_TYPE_HANDLER_MAP) {
            map = new HashMap>();
            TYPE_HANDLER_MAP.put(javaType, map);
        }
        map.put(jdbcType, handler);
    }
    ALL_TYPE_HANDLERS_MAP.put(handler.getClass(), handler);
}

通过类型处理器的泛型,MyBatis 可以得知该类型处理器处理的 Java 类型,不过这种行为可以通过两种方法改变:

  • 在类型处理器的配置元素(typeHandler element)上增加一个 javaType 属性(比如:javaType="String");
  • 在类型处理器的类上(TypeHandler class)增加一个 @MappedTypes 注解来指定与其关联的 Java 类型列表。 如果在 javaType 属性中也同时指定,则注解方式将被忽略。

可以通过两种方式来指定被关联的 JDBC 类型:

  • 在类型处理器的配置元素上增加一个 jdbcType 属性(比如:jdbcType="VARCHAR");
  • 在类型处理器的类上(TypeHandler class)增加一个 @MappedJdbcTypes 注解来指定与其关联的 JDBC 类型列表。 如果在 jdbcType 属性中也同时指定,则注解方式将被忽略。

handlers 添加到 configuration 的 typeHandlerRegistry 属性中

mapperElement(root.evalNode("mappers"));

既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要定义 SQL 映射语句了。但是首先我们需要告诉 MyBatis 到哪里去找到这些语句。 Java 在自动查找这方面没有提供一个很好的方法,所以最佳的方式是告诉 MyBatis 到哪里去找映射文件。你可以使用相对于类路径的资源引用, 或完全限定资源定位符(包括 file:/// 的 URL),或类名和包名等。例如:


  
  

关于 mapper 的解析是一个比较大的工作,下一节讲解。

总结

configuration解析还是比较简单的,主要将配置文件的属性赋值到configuration中供后续使用。

你可能感兴趣的:(Mybatis源码之配置初始化)