Mybatis IO 包源码分析

ClassLoaderWrapper 类是对 ClassLoader 的包装. 怎么理解了?看下这个方法就知道了.

ClassLoader[] getClassLoaders(ClassLoader classLoader) {
return new ClassLoader[]{
classLoader,
defaultClassLoader,
Thread.currentThread().getContextClassLoader(),
getClass().getClassLoader(),
systemClassLoader};
}

实例化一个 Class.
Class<?> classForName(String name, ClassLoader[] classLoader) throws ClassNotFoundException {

for (ClassLoader cl : classLoader) {

  if (null != cl) {

    try {

      Class<?> c = Class.forName(name, true, cl);

      if (null != c) {
        return c;
      }

    } catch (ClassNotFoundException e) {
      // we'll ignore this until all classloaders fail to locate the class
    }

  }

}

throw new ClassNotFoundException("Cannot find class: " + name);

}

查找资源.
URL getResourceAsURL(String resource, ClassLoader[] classLoader) {

URL url;

for (ClassLoader cl : classLoader) {

  if (null != cl) {

    // look for the resource as passed in...
    url = cl.getResource(resource);

    // ...but some class loaders want this leading "/", so we'll add it
    // and try again if we didn't find the resource
    if (null == url) {
      url = cl.getResource("/" + resource);
    }

    // "It's always in the last place I look for it!"
    // ... because only an idiot would keep looking for it after finding it, so stop looking already.
    if (null != url) {
      return url;
    }

  }

}

// didn't find it anywhere.
return null;

}

Resources 是 Mybatis 提供的用于读取资源文件的工具类,我们可以看到它仅仅是对 ClassLoaderWrapper 一层浅浅的封装.

VFS 是虚拟文件系统通用的 API.

VFS 提供了新增用户自定义 VFS 的功能.

看了下 VFS 的实现类,没发现啥重要的东西.

接下来的这个类比较重要. ResolverUtil.

一个测试接口,比如用于判断一个类是否是另一个类的父类,看某个类上是否存在某个注解等.
/**

  • A simple interface that specifies how to test classes to determine if they
  • are to be included in the results produced by the ResolverUtil.
    /
    public interface Test {
    /
    *
    • Will be called repeatedly with candidate classes. Must return True if a class
    • is to be included in the results, false otherwise.
      */
      boolean matches(Class<?> type);
      }

判断 parent 是否是 type 的父类.
public static class IsA implements Test {
private Class<?> parent;

/** Constructs an IsA test using the supplied Class as the parent class/interface. */
public IsA(Class<?> parentType) {
  this.parent = parentType;
}

/** Returns true if type is assignable to the parent type supplied in the constructor. */
@Override
public boolean matches(Class<?> type) {
  return type != null && parent.isAssignableFrom(type);
}

@Override
public String toString() {
  return "is assignable to " + parent.getSimpleName();
}

}

判断 type 上是否存在注解 annotation.
public static class AnnotatedWith implements Test {
private Class<? extends Annotation> annotation;

/** Constructs an AnnotatedWith test for the specified annotation type. */
public AnnotatedWith(Class<? extends Annotation> annotation) {
  this.annotation = annotation;
}

/** Returns true if the type is annotated with the class provided to the constructor. */
@Override
public boolean matches(Class<?> type) {
  return type != null && type.isAnnotationPresent(annotation);
}

@Override
public String toString() {
  return "annotated with @" + annotation.getSimpleName();
}

}

在 packageNames 下查找 parent 的实现类. 将查找到的结果放入 matches 集合.
public ResolverUtil findImplementations(Class<?> parent, String… packageNames) {
if (packageNames == null) {
return this;
}

Test test = new IsA(parent);
for (String pkg : packageNames) {
  find(test, pkg);
}

return this;

}

在 packageNames 下查找被注解 annotation 修饰的类. 将查找到的结果放入 matches 集合.
public ResolverUtil findAnnotated(Class<? extends Annotation> annotation, String… packageNames) {
if (packageNames == null) {
return this;
}

Test test = new AnnotatedWith(annotation);
for (String pkg : packageNames) {
  find(test, pkg);
}

return this;

}

递归查找.
public ResolverUtil find(Test test, String packageName) {
String path = getPackagePath(packageName);

try {
  // 这里用到了 VFS,我们还是看下 VFS 的实现吧.
  List<String> children = VFS.getInstance().list(path);
  for (String child : children) {
    if (child.endsWith(".class")) {
      addIfMatching(test, child);
    }
  }
} catch (IOException ioe) {
  log.error("Could not read package: " + packageName, ioe);
}

return this;

}

再看下 VFS.

VFS 中定义了两个抽象方法,用于子类实现.
// 从 URL 中搜索和 forPath 匹配的路径.
protected abstract List list(URL url, String forPath) throws IOException;
// 判断此 VFS 是否有效.
public abstract boolean isValid();

VFS 类定义好了获取 VFS 实例的逻辑. 优先使用用户自定义的 VFS,其次是系统实现的.
static VFS createVFS() {
// Try the user implementations first, then the built-ins
List<Class<? extends VFS>> impls = new ArrayList<>();
impls.addAll(USER_IMPLEMENTATIONS);
impls.addAll(Arrays.asList((Class<? extends VFS>[]) IMPLEMENTATIONS));

  // Try each implementation class until a valid one is found
  VFS vfs = null;
  for (int i = 0; vfs == null || !vfs.isValid(); i++) {
    Class<? extends VFS> impl = impls.get(i);
    try {
      vfs = impl.getDeclaredConstructor().newInstance();
      if (!vfs.isValid()) {
        if (log.isDebugEnabled()) {
          log.debug("VFS implementation " + impl.getName() +
              " is not valid in this environment.");
        }
      }
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
      log.error("Failed to instantiate " + impl, e);
      return null;
    }
  }

  if (log.isDebugEnabled()) {
    log.debug("Using VFS adapter " + vfs.getClass().getName());
  }

  return vfs;
}

关于 DefaultVFS 是读取 Jar 文件. 详细的就不分析了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源主要含以下内容: ASP项目源码:每个资源含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目都附带了完整的数据库设计文件。这些文件通常括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源都附有详细的开发文档,文档内容括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值