> 文章列表 > 自定义TypeFilter 指定@ComponentScan注解的过滤规则

自定义TypeFilter 指定@ComponentScan注解的过滤规则

自定义TypeFilter 指定@ComponentScan注解的过滤规则

1.FilterType中常用的规则

在使用@ComponentScan注解实现包扫描时,可以使用@Filter指定过滤规则,在@Filter中,通过type来指定过滤的类型。而@Filter注解中的type属性是一个FilterType枚举,其源码如下。

public enum FilterType {/*** Filter candidates marked with a given annotation.* @see org.springframework.core.type.filter.AnnotationTypeFilter*/ANNOTATION,/*** Filter candidates assignable to a given type.* @see org.springframework.core.type.filter.AssignableTypeFilter*/ASSIGNABLE_TYPE,/*** Filter candidates matching a given AspectJ type pattern expression.* @see org.springframework.core.type.filter.AspectJTypeFilter*/ASPECTJ,/*** Filter candidates matching a given regex pattern.* @see org.springframework.core.type.filter.RegexPatternTypeFilter*/REGEX,/** Filter candidates using a given custom* {@link org.springframework.core.type.filter.TypeFilter} implementation.*/CUSTOM
}

1.1.FilterType.ANNOTATION:按照注解进行包含或者排除

例如,使用@ComponentScan注解进行包扫描时,如果要想按照注解只包含标注了@Controller注解的组件,那么就需要像下面这样写。

@ComponentScan(value="com.tianxia.springannotation", includeFilters={/** type:指定你要排除的规则,是按照注解进行排除,还是按照给定的类型进行排除,还是按照正则表达式进行排除,等等* classes:我们需要Spring在扫描时,只包含@Controller注解标注的类*/@ComponentScan.Filter(type=FilterType.ANNOTATION, classes={Controller.class})
}, useDefaultFilters=false)

1.2.FilterType.ASSIGNABLE_TYPE:按照给定的类型进行包含或者排除

使用@ComponentScan注解进行包扫描时,如果要想按照给定的类型只包含BookService类(接口)或其子类(实现类或子接口)的组件,那么就需要像下面这样写。

@ComponentScan(value="com.tianxia.springannotation", includeFilters={// 只要是BookService这种类型的组件都会被加载到容器中,不管是它的子类还是什么它的实现类。记住,只要是BookService这种类型的@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, classes={BookService.class})
}, useDefaultFilters=false)

1.3.FilterType.ASPECTJ:按照ASPECTJ表达式进行包含或者排除

@ComponentScan(value="com.tianxia.springannotation", includeFilters={@Filter(type=FilterType.ASPECTJ, classes={AspectJTypeFilter.class})
}, useDefaultFilters=false)

这种过滤规则基本上不怎么用!

1.4.FilterType.REGEX:按照正则表达式进行包含或者排除

@ComponentScan(value="com.tianxia.springannotation", includeFilters={@ComponentScan.Filter(type=FilterType.REGEX, classes={RegexPatternTypeFilter.class})
}, useDefaultFilters=false)

1.5.FilterType.CUSTOM:按照自定义规则进行包含或者排除

如果实现自定义规则进行过滤时,自定义规则的类必须是org.springframework.core.type.filter.TypeFilter接口的实现类。

该实现类的代码一开始如下所示。

package com.tianxia.springannotation.filter;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;/*** @ComponentScan自定义规则过滤器* @author liqb* @date 2023-04-23 9:10**/
public class MyTypeFilter implements TypeFilter {/*** @ComponentScan的规则匹配* @author liqb* @date 2023-04-23 09:13* @param metadataReader 读取到的当前正在扫描的类的信息* @param metadataReaderFactory 可以获取到其他任何类的信息的(工厂)* @return 当返回true时,表示符合规则,会包含在Spring容器中;当返回false时,表示不符合规则* @throws IOException*/@Overridepublic 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;}
}

实现TypeFilter接口时,需要实现该接口中的match()方法,match()方法的返回值为boolean类型。当返回true时,表示符合规则,会包含在Spring容器中;当返回false时,表示不符合规则,那就是一个都不匹配,自然就都不会被包含在Spring容器中。

然后,使用@ComponentScan注解进行如下配置。

@ComponentScan(value="com.tianxia.springannotation", includeFilters={// 指定新的过滤规则,这个过滤规则是我们自个自定义的,// 过滤规则就是由我们这个自定义的MyTypeFilter类返回true或者false来代表匹配还是没匹配@Filter(type=FilterType.CUSTOM, classes={MyTypeFilter.class})
}, useDefaultFilters=false)