Mybatis源码:@MapperScan解析过程

Mybatis源码:@MapperScan解析过程目录0.说明1.@MapperScan2.MapperScannerRegister3.ClassPathMapperScanner4.MapperFactoryBean0.说明mybatis构建过程主要包括:解析mybatis配置文件,构造Configuration配置类对象和SqlSessionFactory; 利用@MapperScan注册BeanDe…

大家好,又见面了,我是你们的朋友全栈君。

目录

0.说明

1.@MapperScan

2. MapperScannerRegister

3.ClassPathMapperScanner

4. MapperFactoryBean


0.说明


mybatis构建过程主要包括:

  1. 解析mybatis配置文件,构造Configuration配置类对象和SqlSessionFactory;
  2. 利用@MapperScan注册BeanDefinition到BeanFactory工厂中;

其中第一步中创建了Mapper接口代理类,并存储到Configuration中;

下面主要介绍第二步,利用@MapperScan注册BeanDefinition的过程;

1.@MapperScan

/**
 * Use this annotation to register MyBatis mapper interfaces when using Java
 * Config. It performs when same work as {@link MapperScannerConfigurer} via
 * {@link MapperScannerRegistrar}.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
public @interface MapperScan {

  /**
   * Alias for the {@link #basePackages()} attribute. Allows for more concise
   * annotation declarations e.g.:
   * {@code @EnableMyBatisMapperScanner("org.my.pkg")} instead of {@code
   * @EnableMyBatisMapperScanner(basePackages= "org.my.pkg"})}.
   */
  String[] value() default {};

  /**
   * Base packages to scan for MyBatis interfaces. Note that only interfaces
   * with at least one method will be registered; concrete classes will be
   * ignored.
   */
  String[] basePackages() default {};

  /**
   * Type-safe alternative to {@link #basePackages()} for specifying the packages
   * to scan for annotated components. The package of each class specified will be scanned.
   * <p>Consider creating a special no-op marker class or interface in each package
   * that serves no purpose other than being referenced by this attribute.
   */
  Class<?>[] basePackageClasses() default {};

  /**
   * The {@link BeanNameGenerator} class to be used for naming detected components
   * within the Spring container.
   */
  Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

  /**
   * This property specifies the annotation that the scanner will search for.
   * <p>
   * The scanner will register all interfaces in the base package that also have
   * the specified annotation.
   * <p>
   * Note this can be combined with markerInterface.
   */
  Class<? extends Annotation> annotationClass() default Annotation.class;

  /**
   * This property specifies the parent that the scanner will search for.
   * <p>
   * The scanner will register all interfaces in the base package that also have
   * the specified interface class as a parent.
   * <p>
   * Note this can be combined with annotationClass.
   */
  Class<?> markerInterface() default Class.class;

  /**
   * Specifies which {@code SqlSessionTemplate} to use in the case that there is
   * more than one in the spring context. Usually this is only needed when you
   * have more than one datasource.
   */
  String sqlSessionTemplateRef() default "";

  /**
   * Specifies which {@code SqlSessionFactory} to use in the case that there is
   * more than one in the spring context. Usually this is only needed when you
   * have more than one datasource.
   */
  String sqlSessionFactoryRef() default "";

  /**
   * Specifies a custom MapperFactoryBean to return a mybatis proxy as spring bean.
   *
   */
  Class<? extends MapperFactoryBean> factoryBean() default MapperFactoryBean.class;
}

MapperScan注解属性如下:

  1. value、basePackages、basePackageClasses都是用于提供扫描接口类的包名;
  2. BeanNameGenerator作为Bean名称生成器,支持自定义;
  3. annotationClass、markerInterface作为marker标记,限定需要扫描的接口需要满足的条件(包含特定注解或实现某个接口);
  4. sqlSessionTemplateRef、sqlSessionFactoryRef在有多个数据源的情况下指定使用哪一个数据源;
  5. factoryBean,指定FactoryBean实现类,用于生成接口代理类,默认为MapperFactoryBean.class, 支持自定义;

另外MapperScan通过import注解导入了MapperScannerRegistrar类,该接口实现了ImportBeanDefinitionRegistrar接口,用于注册BeanDefinition;

2. MapperScannerRegistrar

MapperScannerRegistrar类结构如下:

Mybatis源码:@MapperScan解析过程

ImportBeanDefinitionRegistrar:是spring开放出来的用于注册其他BeanDefinition的接口,供子类具体实现;

MapperScannerRegistrar:实现ImportBeanDefinitionRegistrar接口,同时依赖ClassPathMapperScanner获取候选bean定义, 并对bean定义进行后处理;

ClassPathMapperScanner:继承ClassPathBeanDefinitionScanner,完成指定包下bean定义的注册;

(传送门 Spring源码:ClassPathBeanDefinitionScanner源码分析)

覆盖的ImportBeanDefinitionRegistrar接口中的registerBeanDefinitions方法具体如下,主要完成了ClassPathMapperScanner的构造,并执行doScan方法进行扫描;

MapperScannerRegistrar.registerBeanDefinitions:

@Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
    ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

    // this check is needed in Spring 3.1
    if (resourceLoader != null) {
      scanner.setResourceLoader(resourceLoader);
    }

    Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");
    if (!Annotation.class.equals(annotationClass)) {
      scanner.setAnnotationClass(annotationClass);
    }

    Class<?> markerInterface = annoAttrs.getClass("markerInterface");
    if (!Class.class.equals(markerInterface)) {
      scanner.setMarkerInterface(markerInterface);
    }

    Class<? extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator");
    if (!BeanNameGenerator.class.equals(generatorClass)) {
      scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass));
    }

    Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");
    if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {
      scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));
    }

    scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));
    scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef"));

    List<String> basePackages = new ArrayList<String>();
    for (String pkg : annoAttrs.getStringArray("value")) {
      if (StringUtils.hasText(pkg)) {
        basePackages.add(pkg);
      }
    }
    for (String pkg : annoAttrs.getStringArray("basePackages")) {
      if (StringUtils.hasText(pkg)) {
        basePackages.add(pkg);
      }
    }
    for (Class<?> clazz : annoAttrs.getClassArray("basePackageClasses")) {
      basePackages.add(ClassUtils.getPackageName(clazz));
    }
    scanner.registerFilters();
    scanner.doScan(StringUtils.toStringArray(basePackages));
  }

 

3.ClassPathMapperScanner

ClassPathMapperScanner主要用于注册指定包名下的bean定义,并对注册的bean定义进行后处理,这里主要是设置beanClass为MapperFactoryBean、设置构造函数参数对象、设置autoWireMode=byType(MapperScan注解属性没有指定时,默认将容器中的DefalultSqlSectionFactory绑定到set方法的入参对象中,构造PropertyValue值),其doScan方法如下:

/**
   * Calls the parent search that will search and register all the candidates.
   * Then the registered objects are post processed to set them as
   * MapperFactoryBeans
   */
  @Override
  public Set<BeanDefinitionHolder> doScan(String... basePackages) {
    Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

    if (beanDefinitions.isEmpty()) {
      logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");
    } else {
      processBeanDefinitions(beanDefinitions);
    }

    return beanDefinitions;
  }

  private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
    GenericBeanDefinition definition;
    for (BeanDefinitionHolder holder : beanDefinitions) {
      definition = (GenericBeanDefinition) holder.getBeanDefinition();

      if (logger.isDebugEnabled()) {
        logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() 
          + "' and '" + definition.getBeanClassName() + "' mapperInterface");
      }

      // the mapper interface is the original class of the bean
      // but, the actual class of the bean is MapperFactoryBean
      definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59
      definition.setBeanClass(this.mapperFactoryBean.getClass());

      definition.getPropertyValues().add("addToConfig", this.addToConfig);

      boolean explicitFactoryUsed = false;
      if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
        definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionFactory != null) {
        definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
        explicitFactoryUsed = true;
      }

      if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
        explicitFactoryUsed = true;
      } else if (this.sqlSessionTemplate != null) {
        if (explicitFactoryUsed) {
          logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
        }
        definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
        explicitFactoryUsed = true;
      }

      if (!explicitFactoryUsed) {
        if (logger.isDebugEnabled()) {
          logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");
        }
        definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
      }
    }
  }

4. MapperFactoryBean

Mybatis源码:@MapperScan解析过程

MapperFactoryBean实现FactoryBean接口,继承SqlSessionDaoSupport,其中SqlSessionDaoSupport有成员变量sqlSession,初始化是在setSqlSessionFactory方法中完成的(setSqlSessionFactory是根据autoWire=byType属性注入进来的)

SqlSessionDaoSupport.setSqlSessionFactory:

public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
    if (!this.externalSqlSession) {
      this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
    }
}

因为MapperFactoryBean是FactoryBean的子类,所以bean实例化时会调用MapperFactoryBean的getObject方法:

@Override
  public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
 }

进而调用sqlSession的getMapper接口,即是sqlSessionTemplete的getMapper方法:

@Override
  public <T> T getMapper(Class<T> type) {
    return getConfiguration().getMapper(type, this);
  }

这样最终就从Configuration中获取mapper接口的代理对象;而Configuration中的mapper接口代理对象是在mybatis配置解析时构造完成的;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141940.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • unity 的Cinemachine组件运用

    unity 的Cinemachine组件运用1.第三人称视角控制通过PackageManager安装CineMachine1) 最简单的方法使用freeLook虚拟相机常用的调整为:1.观察目标:将要看的目标放在这里。2输入控制:把你想用来控制的虚拟轴(就是InputManager里的)的名字输入进去就行。默认是填mouse那个输入轴。注意:似乎不支持NewInputSystem。所以在用NewInputSystem时要么用在projectSetting/player里改成both设置。要么自己写脚本去调用这个组件中的

  • 长轮询的使用实现_python 轮询

    长轮询的使用实现_python 轮询轮询(Polling):是指不管服务器端有没有更新,客户端(通常是指浏览器)都定时的发送请求进行查询,轮询的结果可能是服务器端有新的更新过来,也可能什么也没有,只是返回个空的信息。不管结果如何,客户端处理完后到下一个定时时间点将继续下一轮的轮询。长轮询(LongPolling):长轮询的服务其客户端是不做轮询的,客户端在发起一次请求后立即挂起,一直到服务器端有更新的时候,服务器才会主动推送信息到…

    2022年10月14日
  • java高级面试题及答案

    java高级面试题及答案前言Linux网络协议栈是根据TCP/IP模型来实现的,TCP/IP模型由应用层、传输层、网络层和网络接口层,共四层组成,每一层都有各自的职责。应用程序要发送数据包时,通常是通过socket接口,于是就会发生系统调用,把应用层的数据拷贝到内核里的socket层,接着由网络协议栈从上到下逐层处理后,最后才会送到网卡发送出去。而对于接收网络包时,同样也要经过网络协议逐层处理,不过处理的方向与发送数据时是相反的,也就是从下到上的逐层处理,最后才送到应用程序。网络的速度往往跟用户体验是挂钩

  • linux下rar包的解压方法

    linux下rar包的解压方法一widonds下打包rar文件并上传二下载并安装rar软件三rar命令语法四具体例子五更多命令查看帮助一.widonds下打包rar文件并上传yuminstalllrzszrztest.rar二、下载并安装rar软件2.1下载mkdir-p/home/oldboy/toolscd/home/oldboy/toolswgethttp://www.rarlab.c

  • 常用命令详解-Net命令「建议收藏」

    常用命令详解-Net命令「建议收藏」1.Net常用命令:(1)netshare-查看共享命令netshareipc−设置ipc−设置ipc-设置ipc共享netshareipc/del−删除ipc/del−删除ipc/del-删除ipc共享(xp系统无法删除)netsharec$=c:-设置c盘为共享(2)netuser-查看本地的用户列表netuser用户名密码…

  • Python之json文件

    json简介json是一种轻量级的数据交换格式完全独立于编程语言的文本格式来存储和表示数据简单和清晰的层次结构使得json成为理想的数据交换语言。易于阅读和编写,易于机器解析和生成,并有效地提升

    2021年12月19日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号