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)


相关推荐

  • Oracle数据库备份与恢复方案

    Oracle数据库备份与恢复方案任何数据库在长期使用过程中,都会存在安全隐患。对于数据库管理员来说不能仅寄希望于计算机操作系统的安全运行,而是要建立一整套的数据库备份与恢复机制。当任何人为的或是自然的灾难一旦出现,而导致数据库崩溃、物理介质损坏等,就可以及时恢复系统中重要的数据,不影响整个单位业务的运作。然而如果没有可靠的备份数据和恢复机制,就会带来系统瘫痪、工作停滞、经济损失等等不堪设想的后果。本文以ORACLE数据库为例,结

  • 激光测距芯片VL53L0X的使用与代码

    激光测距芯片VL53L0X的使用与代码一、介绍1、原理采用940nm垂直腔面发射激光器(Vertical-CavitySurface-EmittingLaser,简称VCSEL)发射出激光,激光碰到障碍物后反射回来被VL53L0X接收到,测量激光在空气中的传播时间,进而得到距离。VCSEL相关知识2、参数超小体积:4.4×2.4×1.0mm最大测距:2m发射的激光对眼镜安全,且完全不可见。工作电压:2.6to3.5V通信方式:IIC,400KHz,设备地址0x52,最低位是读…

  • Canny算子–边缘检测[通俗易懂]

    Canny算子–边缘检测[通俗易懂]Canny边缘检测算法的发展历史Canny边缘检测于1986年由JOHNCANNY首次在论文《AComputationalApproachtoEdgeDetection》中提出,就此拉开了Canny边缘检测算法的序幕。Canny边缘检测是从不同视觉对象中提取有用的结构信息并大大减少要处理的数据量的一种技术,目前已广泛应用于各种计算机视觉系统。Canny发现,在不同视觉系统…

  • 【14】进大厂必须掌握的面试题-持续监控面试

    Q1。为什么需要连续监控? 我建议您遵循以下流程:连续监视可以及时发现问题或弱点,并采取快速纠正措施来帮助减少组织的费用。持续监控提供的解决方案可解决以下三个运营准则: 持续审核 …

  • 蓝桥杯真题总结(蓝桥杯考什么)

    Python获取每一位的数字,并返回到列表:方法一:whilevalue:result.append(value%10)value=value//10#逆序,按正常的顺序返回result.reverse()方法二:list(map(int,str(value)))方法三:#divmod()是内置函数,返回整商和余数组成的元组result=[]whilevalue:value,r=divmod(value,1

  • Md编辑器_wife可以看电视但不能打游戏

    Md编辑器_wife可以看电视但不能打游戏文章目录为什么写这个?0.介绍一下md?1纯md语法的使用1.1快捷键1.2字符效果和横线等1.2.1横线1.2.2删除线1.2.3斜体字1.2.4粗体1.2.5粗斜体1.2.6上标与下标1.2.7**缩写(同HTML的abbr标签)**1.2.8引用Blockquotes1.3各级标签标签1标签2标签3标签4标签5标签61.4.列表1.4.1无序列表(…

发表回复

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

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