gpt4 book ai didi

spring - 如何复制Spring的component-scan

转载 作者:行者123 更新时间:2023-12-05 01:15:53 26 4
gpt4 key购买 nike

我想在基于 Spring 的 Web 应用程序中搜索一些注释,例如 @Entity。因此,当服务器启动时,我需要与 Spring 相同的功能,它会查找所有用 @Component 注释的类。在我的例子中,我不创建单例,收集所有那些用 @Entity 注释的类对我来说很重要。

是否有可能为此使用现有的 Spring 工具?我想在与 Spring 为 @Component 注释所做的相同命名空间中进行搜索。

最佳答案

当然,看parse() org.springframework.context.annotation.ComponentScanBeanDefinitionParser 中的方法.当Spring遇到<context:component-scan/>时调用该方法在 XML 配置中。您可能可以对其进行一些剥离以更好地满足您的需求,但它应该作为一个综合示例。

你应该特别感兴趣的类是org.springframework.context.annotation.ClassPathBeanDefinitionScanner .来自 JavaDoc:

Candidate classes are detected through configurable type filters. The default filters include classes that are annotated with Spring's @Component, @Repository, @Service, or @Controller stereotype.

顺便说一句,如果您需要不太通用的解决方案,也许您的持久性提供程序有一些 API 来获取所有实体类?

关于spring - 如何复制Spring的component-scan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5337660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com