通过某些组件名称模式将组件/类加载到 ArrayList 中
我有许多相同类型的类(com.project.abc.abs.Agent
),注释如下;
@Component("WEB.Agent-1")、@Component("WEB.Agent-2")、@Component("WEB.Agent-3")
...等等。现在假设所有类都在同一个包中 (com.project.abc.web.Agent1...
)。
这些类都是单例,我想将它们动态加载到中央“代理管理器”类中。即,每次使用 @Component("WEB.Agent-#")
注释添加新的代理类时,都会拾取该类,而无需对代理管理器进行更改。在 AgentManager 类中,我需要某种方法来加载与名称“WEB.Agent-#”(其中 # 是数字或某个唯一 ID)匹配的任何组件,这可以使用 Spring 中的任何方法吗?
如果不是,我假设我需要从特定文件夹/包加载所有类?
I have a number of classes of the same type (com.project.abc.abs.Agent
) annotated like so;
@Component("WEB.Agent-1"), @Component("WEB.Agent-2"), @Component("WEB.Agent-3")
... etc. For now assume that all classes are in the same package (com.project.abc.web.Agent1...
).
These classes are all singletons, and I want to load them dynamically into a central 'Agent manager' class. I.e. every time a new agent class is added with the @Component("WEB.Agent-#")
annotation it is picked up without having to make changes to the Agent Manager. In the AgentManager class I need some method to load any component that matches the name "WEB.Agent-#" (where # is a number or some unique ID) is this possible using any methods in Spring?
If not I'm assuming that I would need to go about loading all classes from a particular folder/package?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ClassPathScanningCandidateComponentProvider
并添加一个排除过滤器,以删除与您的模式不匹配的内容:You can do this with
ClassPathScanningCandidateComponentProvider
and add an exclude filter that gets rid of things that don't match your pattern: