英文:
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<Class<?>> 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论