zoukankan      html  css  js  c++  java
  • 4、组件注册-自定义TypeFilter指定过滤规则

    4、组件注册-自定义TypeFilter指定过滤规则

    4.1 FilterType.ANNOTATION 按照注解方式

    4.2 FilterType.ASSIGNABLE_TYPE 按照给定的类型

    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class})
    

    4.3 FilterType.ASPECTJ 按照ASPECTJ表达式

    4.4 FilterType.REGEX 按照正则表达

    4.5 FilterType.CUSTOM 按照自定义规则

    @ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
            includeFilters = {
    //        @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
    //                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),
                    @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
            }
    )       // 指定只扫描
    
    
    • 创建MyTypeFilter类,需要实现TypeFilter 接口,重写match()方法
    package com.hw.springannotation.config;
    
    import org.springframework.core.io.Resource;
    import org.springframework.core.type.AnnotationMetadata;
    import org.springframework.core.type.ClassMetadata;
    import org.springframework.core.type.classreading.MetadataReader;
    import org.springframework.core.type.classreading.MetadataReaderFactory;
    import org.springframework.core.type.filter.TypeFilter;
    
    import java.io.IOException;
    
    /**
     * @Description TODO
     * @Author hw
     * @Date 2018/11/27 18:49
     * @Version 1.0
     */
    public class MyTypeFilter implements TypeFilter {
        /**
         * Determine whether this filter matches for the class described by
         * the given metadata.
         *
         * @param metadataReader        the metadata reader for the target class    读取到得当前正在扫描的类的信息
         * @param metadataReaderFactory a factory for obtaining metadata readers    一个可以探索其他类信息的工厂类
         *                              for other classes (such as superclasses and interfaces)
         * @return whether this filter matches
         * @throws IOException in case of I/O failure when reading metadata
         */
        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
            // 获取当前类的注解信息
            AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
            // 获取当前类信息
            ClassMetadata classMetadata = metadataReader.getClassMetadata();
            // 获取当前类的资源(类的路径)
            Resource resource = metadataReader.getResource();
    
            String className = classMetadata.getClassName();
            System.out.println("----" + className);
    
            if (className.contains("er")) {
                return true;
            }
            return false;
        }
    }
    
    

  • 相关阅读:
    DataGridView编辑后立即更新到数据库的两种方法
    DataTable 转换成 Json的3种方法
    C# 应用程序配置文件App.Config和web.config
    C#中使用JsonConvert解析JSON
    C#JsonConvert.DeserializeObject反序列化json字符
    WIN10远程桌面、常用命令
    control[控制面板]的参数
    win10企业版变成win10专业版的设置教程
    DLL加密
    微信小程序顶部tab
  • 原文地址:https://www.cnblogs.com/Grand-Jon/p/10018643.html
Copyright © 2011-2022 走看看