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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 详解BP神经网络

    BackPropagationNeuronNetWok  BP神经网络学习算法可以说是目前最成功的神经网络学习算法。显示任务中使用神经网络时,大多数是使用BP算法进行训练.  在我看来BP神经网络就是一个”万能的模型+误差修正函数“,每次根据训练得到的结果与预想结果进行误差分析,进而修改权值和阈值,一步一步得到能输出和预想结果一致的模型。举一个例子:比如某厂商生产一种产品,投放到市场之…

    2022年4月6日
    58
  • LaTeX入门级教程

    LaTeX入门级教程    LaTeX(LATEX,音译“拉泰赫”)是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(LeslieLamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天,甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类…

    2022年7月16日
    13
  • 谷歌浏览器驱动测试

    谷歌浏览器驱动测试selenium驱动谷歌浏览器,ip+headless+不出现自动测试字样importtimefromseleniumimportwebdriverfromselenium.webdriverimportChromeOptionsfromselenium.webdriver.chrome.optionsimportOptionsimportrequestsdefget_proxy():proxy=requests.get(“http://127.0.0

    2022年6月8日
    31
  • 计算机网络——DHCP协议详解

    计算机网络——DHCP协议详解由浅入深理解DHCP协议

    2022年5月10日
    67
  • shell 循环语句[通俗易懂]

    shell 循环语句[通俗易懂]循环语句:for语法结构:1、列表循环forvariablein{list}docommand…doneforvariableinvar1var2var3..docommand…done2、非列表循环forvariabledocommand..done3、类C风

    2022年7月24日
    10
  • vue的$attrs_vue获取list集合中的对象

    vue的$attrs_vue获取list集合中的对象​说明本文用示例介绍Vue的$attrs和$listener的用法官网API—Vue.js$attrs和$listeners介绍Vue2.4中,引入了attrs和listeners,新增了inheritAttrs选项。$attrs:包含了父作用域中没有被prop接收的所有属性(不包含class和style属性)。可以通过v-bind=”$attrs”直接将这些属性传入内部组件。$

    2022年8月31日
    5

发表回复

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

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