zoukankan      html  css  js  c++  java
  • SpringMyBatis解析4-MapperScannerConfigurer

    如果有成百上千个dao接口呢,那我们岂不是要配置添加成百上千个bean,当然不是这样,spring还为MyBatis添加了拓展的功能,可以通过扫描包目录的方式,添加dao,让我看看具体使用和实现。

    <!-- 去掉该配置  
    <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean">  
        <property name="mapperInterface" value="net.itaem.dao.PersonDao"/>  
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>  
    </bean>  
     -->  
     <!-- 如果 net.itaem.dao 包下面有很多dao需要注册,那么可以使用这种扫描的方式添加dao-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="net.itaem.dao"/>  
    </bean>  

    我们屏蔽掉了最原始的代码(userMapper 的创建)而增加了MapperScannerConfigurer的配置,basePackage属性是让你为映射器接口文件设置基本的包路径。你可以使用分号或逗号作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。被发现的映射器将会使用Spring对自动侦测组件默认的命名策略来命名。也就是说,如果没有发现注解,它就会使用映射器的非大写的非完全限定类名。但是如果发现了@Component或JSR-330@Named注解,它会获取名称。

    public void afterPropertiesSet() throws Exception {
      notNull(this.basePackage, "Property 'basePackage' is required");
    }

    afterPropertiesSet()方法除了一句对basePackage属性的验证代码外并没有太多的逻辑实现。

    MapperScannerConfigurer实现了BeanDefinitionRegistryPostProcessor接口,如果MapperScannerConfigurer实现了该接口,那么说明在application初始化的时候该接口会被调用,具体实现,让我先看看:

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {  
      if (this.processPropertyPlaceHolders) {  
        processPropertyPlaceHolders();  
      }  
      
      ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);  
      scanner.setAddToConfig(this.addToConfig);  
      scanner.setAnnotationClass(this.annotationClass);  
      scanner.setMarkerInterface(this.markerInterface);  
      scanner.setSqlSessionFactory(this.sqlSessionFactory);  
      scanner.setSqlSessionTemplate(this.sqlSessionTemplate);  
      scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);  
      scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);  
      scanner.setResourceLoader(this.applicationContext);  
      scanner.setBeanNameGenerator(this.nameGenerator);  
      scanner.registerFilters();  
      scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));  
    }  
    这里我们重点关注三个主要的方法,分别是
    processPropertyPlaceHolders(); 
    scanner.registerFilters();
    scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));

    processPropertyPlaceHolders属性的处理

    执行属性的处理,简单的说,就是把xml中${XXX}中的XXX替换成属性文件中的相应的值

      private void processPropertyPlaceHolders() {
        Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
    
        if (!prcs.isEmpty() && applicationContext instanceof GenericApplicationContext) {
          BeanDefinition mapperScannerBean = ((GenericApplicationContext) applicationContext)
              .getBeanFactory().getBeanDefinition(beanName);
    
          // PropertyResourceConfigurer does not expose any methods to explicitly perform
          // property placeholder substitution. Instead, create a BeanFactory that just
          // contains this mapper scanner and post process the factory.
          DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
          factory.registerBeanDefinition(beanName, mapperScannerBean);
    
          for (PropertyResourceConfigurer prc : prcs.values()) {
            prc.postProcessBeanFactory(factory);
          }
    
          PropertyValues values = mapperScannerBean.getPropertyValues();
    
          this.basePackage = updatePropertyValue("basePackage", values);
          this.sqlSessionFactoryBeanName = updatePropertyValue("sqlSessionFactoryBeanName", values);
          this.sqlSessionTemplateBeanName = updatePropertyValue("sqlSessionTemplateBeanName", values);
        }
      }

    BeanDefinitionRegistries会在应用启动的时候调用,并且会早于BeanFactoryPostProcessors的调用,这就意味着PropertyResourceConfigurers还没有被加载所有对于属性文件的引用将会失效。为避免此种情况发生,此方法手动地找出定义的PropertyResourceConfigurers并进行提前调用以保证对于属性的引用可以正常工作。

     

    根据配置属性生成过滤器

    scanner.registerFilters();方法会根据配置的属性生成对应的过滤器,然后这些过滤器在扫描的时候会起作用。

      public void registerFilters() {
        boolean acceptAllInterfaces = true;
        //对于annotationClass属性的处理
        if (this.annotationClass != null) {
          addIncludeFilter(new AnnotationTypeFilter(this.annotationClass));
          acceptAllInterfaces = false;
        }
        //对于markerInterface属性的处理
        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;
            }
          });
        }
        //不扫描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");
          }
        });
      }

    代码中得知,根据之前属性的配置生成了对应的过滤器。

    (1)annotationClass属性处理。

    如果annotationClass不为空,表示用户设置了此属性,那么就要根据此属性生成过滤器以保证达到用户想要的效果,而封装此属性的过滤器就是AnnotationTypeFilter。AnnotationTypeFilter保证在扫描对应Java文件时只接受标记有注解为annotationClass的接口。

    (2)markerInterface属性处理。

    如果markerInterface不为空,表示用户设置了此属性,那么就要根据此属性生成过滤器以保证达到用户想要的效果,而封装此属性的过滤器就是实现AssignableTypeFilter接口的局部类。表示扫描过程中只有实现markerInterface接口的接口才会被接受。

    (3)全局默认处理。

    在上面两个属性中如果存在其中任何属性,acceptAllInterfaces的值将会改变,但是如果用户没有设定以上两个属性,那么,Spring会为我们增加一个默认的过滤器实现TypeFilter接口的局部类,旨在接受所有接口文件。

    (4)package-info.java处理。

    对于命名为package-info的Java文件,默认不作为逻辑实现接口,将其排除掉,使用TypeFilter接口的局部类实现match方法。

    从上面的函数我们看出,控制扫描文件Spring通过不同的过滤器完成,这些定义的过滤器记录在了includeFilters和excludeFilters属性中。

    扫描java文件

    scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));

      public int scan(String... basePackages) {
        int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
        doScan(basePackages);
        //如果配置了includeAnnotationConfig,则注册对应注解的处理器以保证注解功能的正常使用。
        if (this.includeAnnotationConfig) {
          AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
        }
        return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
      }
      //scan是个全局方法,扫描工作通过doScan(basePackages)委托给了doScan方法,同时,还包括了includeAnnotationConfig属性的处理,
      //AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry)代码主要是完成对于注解处理器的简单注册,
      //比如AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor等,这里不再赘述,我们重点研究文件扫描功能的实现。
      protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
        //如果没有扫描到任何文件发出警告
        Assert.notEmpty(basePackages, "At least one base package must be specified");
        Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>();
        //扫描basePackage路径下java文件
        for (String basePackage : basePackages) {
          //如果当前bean是用于生成代理的bean那么需要进一步处理
          //findCandidateComponents方法根据传入的包路径信息并结合类文件路径拼接成文件的绝对路径,
          //同时完成了文件的扫描过程并且根据对应的文件生成了对应的bean,
        //使用ScannedGenericBeanDefinition类型的bean承载信息,bean中只记录了resource和source信息。
    //我们更感兴趣的是isCandidateComponent(metadataReader),此句代码用于判断当前扫描的文件是否符合要求,
        //而我们之前注册的一些过滤器信息也正是在此时派上用场的。
    Set<BeanDefinition> candidates = findCandidateComponents(basePackage); for (BeanDefinition candidate : candidates) { //解析scope属性 ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate); candidate.setScope(scopeMetadata.getScopeName()); String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry); if (candidate instanceof AbstractBeanDefinition) { postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); } //如果是AnnotatedBeanDefinition类型的bean,需要检测下常用注解如:Primary、Lazy等 if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } //检测当前bean是否已经注册 if (checkCandidate(beanName, candidate)) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); beanDefinitions.add(definitionHolder); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; }
    该方法主要做了以下操作:
    1)扫描basePackage下面的java文件
    2)解析扫描到的java文件
    3)调用各个在上一步骤注册的过滤器,执行相应的方法。
    4)为解析后的java注册bean,注册方式采用编码的动态注册实现。
    5)构造MapperFactoryBean的属性,mapperInterface,sqlSessionFactory等等,填充到BeanDefinition里面去。
    做完这些,MapperFactoryBean对象也就构造完成了,扫描方式添加dao的工作也完成了。

    总结

    其实了解了Spring整合MyBatis的流程,我们也就大体知道Spring整合一些框架所使用的扩展方法,不过大多是都是通过继承接口的方式,然后通过spring回调该接口的方式,实现我们自己想要的扩展逻辑,所以了解spring提供的一些扩展的接口以及抽象类是扩展的关键,就像InitializingBean,BeanDefinitionRegistryPostProcessor这些接口,知道了这些接口调用的方式,以及上面时候会调用,我们就可以知道,我们需要扩展的功能应该实现哪个接口,或者集成哪个抽象类。
  • 相关阅读:
    问题及解决:使用dotnet publish发布时Visual Stuido创建的配置文件中的路径失效
    模式的定义
    Identity Server 4 从入门到落地(三)—— 创建Web客户端
    信息系统的不能和能
    虚拟机中CentOS 6.8 Linux搭建GitLab服务器(安装篇)
    Eclipse快捷键大全
    冒泡排序实现
    JAVA的数据类型
    IDEA快捷键大全(翻译自官方手册)
    IntelliJ IDEA入门设置指南
  • 原文地址:https://www.cnblogs.com/wade-luffy/p/6094236.html
Copyright © 2011-2022 走看看