Mybatis从入门到放弃系列:IO操作(4)

ResolverUtil


前面我们讲到了ResolverUtil是用来找到符合条件的类。下面我们具体来讲讲怎么用的

Mybatis从入门到放弃系列:IO操作(4)

只有3个方法

  • 第一个是根据包路径和Test类型的类找到符合条件的类
  • 第二个是根据包名和指定的注解找到符合类上面有该注解的类
  • 第三个是根据包名和指定的类找到该类或者该类的子类

是不是比较简单?

我们再来看看Test类的定义

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

一个方法。

再来看看Test的两个子类实现

public static class IsA implements Test {
  private Class<?> parent;

  public IsA(Class<?> parentType) {
    this.parent = parentType;
  }

  @Override
  public boolean matches(Class<?> type) {
    return type != null && parent.isAssignableFrom(type);
  }

}
public static class AnnotatedWith implements Test {
  private Class<? extends Annotation> annotation;

  public AnnotatedWith(Class<? extends Annotation> annotation) {
    this.annotation = annotation;
  }


  @Override
  public boolean matches(Class<?> type) {
    return type != null && type.isAnnotationPresent(annotation);
  }

}

是不是比较简单,所以,如果需要自定义实现规则的话,继承Test类,实现matches方法就行了,也比较简单。

关注我,带你由浅入深,走入源码世界。

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章