如何通过类路径中的多个注释找到 类?

How to find classes by multiple annotations from classpath?

我想从类路径中找到所有 类 定义了两个注释的。

通常当需要其中一个注释时,我可以按如下方式查找它们( 匹配):

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
provider.addIncludeFilter(new AnnotationTypeFilter(CustomAnnotation.class));
Set<BeanDefinition> beans = provider.findCandidateComponents("com.xyz.abc");

如果我需要 AND 匹配,我需要更改什么,以便只返回具有 both 注释的 类 ?

您是否考虑过创建自己的过滤器?类似于:

final TypeFilter entityFilter = new AnnotationTypeFilter(Entity.class);
final TypeFilter customFilter = new AnnotationTypeFilter(CustomAnnotation.class);
TypeFilter andFilter = new TypeFilter {
    boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        return entityFilter.match(metadataReader, metadataReaderFactory) && customFilter.match(metadataReader, metadataReaderFactory);
    }
};
provider.addIncludeFilter(andFilter);