EntityScanner能检测到抽象类和接口吗?(Spring Boot 2.2.6)

huangapple go评论95阅读模式
英文:

Does EntityScanner detect abstract classes and interfaces? (Spring Boot 2.2.6)

问题

EntityScanner会检测到使用特定注解类型进行注解的抽象类或接口吗?在我的Spring Boot应用程序中,我使用一个EntityScanner实例来查找所有带有特定注解(例如@MyAnnotationType)的类。然而,它不会检测到带有该注解的抽象类或接口:

@MyAnnotationType
public interface Foo

当我调用Set<Class<?>> entityClassSet = entityScanner.scan(MyAnnotationType.class)时,Foo接口不会包含在返回的entityClassSet中。带有@MyAnnotationType注解的抽象类也是同样的情况。请注意,所有这些类都位于@SpringBootApplication类包的子包中。这是预期的行为吗?如果是这样,是否有替代的方法来检测这些抽象类/接口,而不使用EntityScanner?非常感谢!

英文:

Does the EntityScanner detect abstract classes or interfaces that are annotated with the specific annotation type the scanner is looking for? In my Spring Boot application I use an EntityScanner instance to find all classes that have a certain annotation (eg. @MyAnnotationType). However, it doesn't detect abstract classes or interfaces with that annotation:

@MyAnnotationType
public interface Foo

When I call Set&lt;Class&lt;?&gt;&gt; entityClassSet = entityScanner.scan(MyAnnotationType.class) the Foo interface is not included in the returned entityClassSet. The same thing happens to abstract classes with the @MyAnnotationType annotation. Note that all the classes are in the subpackage of the @SpringBootApplication classes package. Is this expected behaviour? If so, is there an alternative to EntityScanner that I can use to detect these abstract classes/interfaces? Thanks a lot!

答案1

得分: 1

不,它并不会。 EntityScanner 仅用于检测类。从 EntityScanner 的实现可以看出,在内部它使用了 ClassPathScanningCandidateComponentProvider

如果您需要扫描抽象类或接口,您可以使用那个。在这样做时,您将不得不重写 isCandidateComponent

英文:

No it does not. EntityScanner is meant to detect classes only. Looking at the implementation of EntityScanner, you can see it uses ClassPathScanningCandidateComponentProvider under the hood.

If you need to scan for abstract classes or interfaces, you could use that. When doing so, you'll have to override isCandidateComponent.

huangapple
  • 本文由 发表于 2020年5月30日 03:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/62093295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定