mybatis @MapperScan 解析

mybatis @MapperScan 解析MapperScan注解会引入MapperScannerRegistrar,MapperScannerRegistrar实现了ImportBeanDefinitionRegistrar,可以向beanFactory中注册BeanDefinition,具体注入的过程是通过ClassPathMapperScanner实现的。publicvoidregisterBeanDefi…

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

MapperScan 注解会引入 MapperScannerRegistrar,MapperScannerRegistrar 实现了 ImportBeanDefinitionRegistrar,可以向 beanFactory 中 注册 BeanDefinition,具体注入的过程是通过 ClassPathMapperScanner 实现的。

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

获取注解信息设置到 ClassPathMapperScanner 中,其中 annotationClass 是在扫描的时候只有标注了 annotationClass 的类会被加入到 factory 中。markerInterface 设置父接口,如果设置了那么只有这个markerInterface的子类接口再回被加入到 factory 中。具体的设置在 ClassPathMapperScanner 中的 registerFilters 方法中。

public void registerFilters() {
    boolean acceptAllInterfaces = true;

    // if specified, use the given annotation and / or marker interface
    if (this.annotationClass != null) {
      addIncludeFilter(new AnnotationTypeFilter(this.annotationClass));
      acceptAllInterfaces = false;
    }

    // override AssignableTypeFilter to ignore matches on the actual marker interface
    if (this.markerInterface != null) {
      addIncludeFilter(new AssignableTypeFilter(this.markerInterface) {
        @Override
        protected boolean matchClassName(String className) {
          return false;
        }
      });
      acceptAllInterfaces = false;
    }

    if (acceptAllInterfaces) {
      // default include filter that accepts all classes
      addIncludeFilter(new TypeFilter() {
        @Override
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
          return true;
        }
      });
    }

    // exclude package-info.java
    addExcludeFilter(new TypeFilter() {
      @Override
      public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        String className = metadataReader.getClassMetadata().getClassName();
        return className.endsWith("package-info");
      }
    });
  }

可以看出在最后排除了 package-info 的 class 类。ClassPathMapperScanner 其他的方法

  @Override
  protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
    return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
  }

表示只会扫描 接口interface 不会扫描 class,第二个方法是表示 当前类可以单独实例化。

protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) {
    if (super.checkCandidate(beanName, beanDefinition)) {
      return true;
    } else {
      logger.warn("Skipping MapperFactoryBean with name '" + beanName 
          + "' and '" + beanDefinition.getBeanClassName() + "' mapperInterface"
          + ". Bean already defined with the same name!");
      return false;
    }
  }

调用父类的方法,

protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException {
		if (!this.registry.containsBeanDefinition(beanName)) {
			return true;
		}
		BeanDefinition existingDef = this.registry.getBeanDefinition(beanName);
		BeanDefinition originatingDef = existingDef.getOriginatingBeanDefinition();
		if (originatingDef != null) {
			existingDef = originatingDef;
		}
		if (isCompatible(beanDefinition, existingDef)) {
			return false;
		}
		throw new ConflictingBeanDefinitionException("Annotation-specified bean name '" + beanName +
				"' for bean class [" + beanDefinition.getBeanClassName() + "] conflicts with existing, " +
				"non-compatible bean definition of same name and class [" + existingDef.getBeanClassName() + "]");
	}

当前类主要的方法是。

private void processBeanDefinitions(Set 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);
  }
}}

对扫描的每一个 interface 进行处理。
设置bean class 是 MapperFactoryBean,设置 构造函数参数当前扫描到的interface 也就是我们写的 mapper 接口interface 作为 MapperFactoryBean 构造函数的参数。设置 MapperFactoryBean 中的 sqlSessionFactory 属性和sqlSessionTemplate属性,最后设置注入属性 AbstractBeanDefinition.AUTOWIRE_BY_TYPE,就是我们在 serviceImpl 中 autowrite mapper 接口。
下面看 MapperFactoryBean ,这个类实现了 spring FactoryBean 接口。
构造函数

public MapperFactoryBean(Class<T> mapperInterface) {
    this.mapperInterface = mapperInterface;
  }

保存我们自己的 mapper 接口。

protected void checkDaoConfig() {
    super.checkDaoConfig();

    notNull(this.mapperInterface, "Property 'mapperInterface' is required");

    Configuration configuration = getSqlSession().getConfiguration();
    if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
      try {
        configuration.addMapper(this.mapperInterface);
      } catch (Exception e) {
        logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
        throw new IllegalArgumentException(e);
      } finally {
        ErrorContext.instance().reset();
      }
    }
  }

会被 spring 回调在

public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
        this.checkDaoConfig();

        try {
            this.initDao();
        } catch (Exception var2) {
            throw new BeanInitializationException("Initialization of DAO failed", var2);
        }
    }

在 DaoSupport 中,主要是完成 mapper xml文件的解析或者 mapper 上注解的解析。
主要是看 getObject 方法,这个是 FactoryBean 定义的。真正被注入到容器中的是这个方法获取的 Object

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

来到 configuration 中的getMapper 方法

  public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }
  protected T newInstance(MapperProxy<T> mapperProxy) {
    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
  }

  public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

最终来到MapperProxyFactory 中,获取到的是jdk 动态代理的类。

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/133676.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 20191124_射雕侠侣和天龙八部小说分类

    20191124_射雕侠侣和天龙八部小说分类关键字提取importpandasaspd#载入语料raw=pd.read_csv(“data/附件2_金庸-射雕英雄传txt精校版.txt”,names=[‘txt’],sep=’aaa’,encoding=”utf-8″,engine=’python’)#章节判断用变量预处理defm_head(tmpstr):…

    2022年7月12日
    20
  • Drone2Map:如何使用带有POS信息的无人机数据生成三维模型「建议收藏」

    Drone2Map:如何使用带有POS信息的无人机数据生成三维模型「建议收藏」使用Drone2Map生成slpk,将slpk加载至ArcGISPro中,slpk悬浮在空中。首先想到的是在pro中调整一下模型高度不就行了,遗憾的是slpk格式是压缩包,不支持模型高度的调整,所以,就必须追根溯源,考虑在Drone2Map生成三维模型的过程中如何解决此问题。 问题分析:一般用户拿到的无人机数据,基本分为两种,一种是无人机拍摄的照片自身带有xyz

    2022年8月15日
    3
  • ldc1614 c语言编程,LDC1614读回来的数据为固定值不变[通俗易懂]

    ldc1614 c语言编程,LDC1614读回来的数据为固定值不变[通俗易懂]OtherPartsDiscussedinThread:LDC1614,LDC1314,LDC1614EVM求教一下各位前辈,硬件是用的LDC1614的评估板,ch0和ch1上接了两个线圈,并联的电容为100pF,器件ID和装配ID读出来为0x3055和0x5449,和手册上的一致,而且我写寄存器再读出来数据都是对的,排除了软件驱动上的问题,现在可能是配置上有哪里不对,或者芯片有问题(…

    2022年6月7日
    36
  • webservice实例java_Java WebService(实战) 简单实例

    webservice实例java_Java WebService(实战) 简单实例一、准备工作(以下为本实例使用工具)1、MyEclipse10.7.12、JDK1.6.0_22二、创建服务端1、创建【WebServiceProject】,命名为【TheService】。2、创建【Class】类,命名为【ServiceHello】,位于【com.hyan.service】包下。3、编写供客户端调用的方法,即编译方法代码。4、进行编译说明:编译失败的话,请将该项目引用的jd…

    2022年7月21日
    12
  • c# mysql executenonquery_C#与数据库访问技术总结(八)之ExecuteNonQuery方法

    c# mysql executenonquery_C#与数据库访问技术总结(八)之ExecuteNonQuery方法ExecuteNonQuery方法ExecuteNonQuery方法主要用来更新数据。通常使用它来执行Update、Insert和Delete语句。该方法返回值意义如下:对于Update、Insert和Delete语句,返回值为该命令所影响的行数。对于所有其他类型的语句,返回值为-1。Command对象通过ExecuteNonQuery方法更新数据库的过程非常简单,需要进行的步骤如下:(1)创建数…

    2022年9月11日
    0
  • Apache、Nginx、IIS防盗链方法「建议收藏」

    Apache、Nginx、IIS防盗链方法「建议收藏」防盗链?要实现防盗链,我们就必须先理解盗链的实现原理,提到防盗链的实现原理就不得不从HTTP协议说起,在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件。一来可以追溯上一个入站地址是什么,二来对于资源文件,可以跟踪到包含显示他

    2022年7月23日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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