无法使用 spring 返回所有标有特定注释的类 类路径扫描候选组件提供程序



我有遗留的spring(不是boot)应用程序和我想用特定的注释标记所有类。为了实现它,我使用以下答案:https://stackoverflow.com/a/1415338/2674303

所以我创建了以下类:
@Component
public class MyVerifier {
public void verify() {
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(MyAnnotation.class));
Set<BeanDefinition> set =  scanner.findCandidateComponents("base.pack")
}

在代码中我有:

package base.pack.ololo.qwery;
@MyAnnotation
public class MyClass...

package base.pack.another.packagename;
@MyAnnotation
public class MyOtherClass...

set变量为空集。为什么?

如何修复?

我有注释保留策略的问题,所以在运行时不能使用注释

老(破碎)版本

@Inherited
@Target({ ElementType.TYPE })
public @interface MyAnnotation{
}

新(工作)版本:

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface MyAnnotation{
}

最新更新