通过某些组件名称模式将组件/类加载到 ArrayList 中

发布于 2024-12-07 19:02:54 字数 471 浏览 0 评论 0原文

我有许多相同类型的类(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-12-14 19:02:54

您可以使用 ClassPathScanningCandidateComponentProvider 并添加一个排除过滤器,以删除与您的模式不匹配的内容:

ClassPathScanningCandidateComponentProvider scanner =
    new ClassPathScanningCandidateComponentProvider();

scanner.addIncludeFilter(new AnnotationTypeFilter(Component.class));
scanner.addExcludeFilter(new TypeFilter(){
    public boolean match(MetadataReader metadataReader, 
                         MetadataReaderFactory metadataReaderFactory){
        return metadataReader.getAnnotationMetadata()
                 .getAnnotationAttributes(Component.class.getName())
                 .get("value").matches("WEB.Agent-[0-9]+");
    }
});

for (BeanDefinition bd : scanner.findCandidateComponents("com.project.abc.web.Agent1"))
    System.out.println(bd.getBeanClassName());

You can do this with ClassPathScanningCandidateComponentProvider and add an exclude filter that gets rid of things that don't match your pattern:

ClassPathScanningCandidateComponentProvider scanner =
    new ClassPathScanningCandidateComponentProvider();

scanner.addIncludeFilter(new AnnotationTypeFilter(Component.class));
scanner.addExcludeFilter(new TypeFilter(){
    public boolean match(MetadataReader metadataReader, 
                         MetadataReaderFactory metadataReaderFactory){
        return metadataReader.getAnnotationMetadata()
                 .getAnnotationAttributes(Component.class.getName())
                 .get("value").matches("WEB.Agent-[0-9]+");
    }
});

for (BeanDefinition bd : scanner.findCandidateComponents("com.project.abc.web.Agent1"))
    System.out.println(bd.getBeanClassName());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文