Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何获得所有特殊类型的自注入bean?_Spring_Spring Boot_Spring Bean - Fatal编程技术网

Spring 如何获得所有特殊类型的自注入bean?

Spring 如何获得所有特殊类型的自注入bean?,spring,spring-boot,spring-bean,Spring,Spring Boot,Spring Bean,我想构建一个Spring应用程序,在这里可以轻松地添加新组件,而无需太多配置。例如:您有不同类型的文档。这些文档应该能够导出为不同的文件格式 要使此功能易于维护,它(基本上)应按以下方式工作: @Component public class ExcelExporter implements Condition { @PostConstruct public void init() { excelExporter(); } @Bean p

我想构建一个Spring应用程序,在这里可以轻松地添加新组件,而无需太多配置。例如:您有不同类型的文档。这些文档应该能够导出为不同的文件格式

要使此功能易于维护,它(基本上)应按以下方式工作:

@Component
public class ExcelExporter implements Condition {

    @PostConstruct
    public void init() {
        excelExporter();
    }

    @Bean
    public Exporter excelExporter(){
        Exporter exporter= new ExcelExporter();
        return exporter; 
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return true;
    }
}
  • 有人编写了文件格式导出程序
  • 他/她编写了一个组件,用于检查文件格式导出器是否已获得许可(基于Spring条件)。如果导出程序获得许可,则会在应用程序上下文中注入专门的Bean
  • “整个rest”基于注入的bean动态工作。不需要触摸任何东西就可以在GUI上显示它,等等
我的想象是这样的:

@Component
public class ExcelExporter implements Condition {

    @PostConstruct
    public void init() {
        excelExporter();
    }

    @Bean
    public Exporter excelExporter(){
        Exporter exporter= new ExcelExporter();
        return exporter; 
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return true;
    }
}
为了与这些出口商合作(展示它们等),我需要获得所有这些产品。我试过这个:

    Map<String, Exporter> exporter =BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, Exporter.class, true, true);
Map exporter=BeanFactoryUtils.beansoftypeincludingOriends(appContext,exporter.class,true,true);

不幸的是,这不起作用(返回0个bean)。我是一个新手,有谁能告诉我如何在春天做好这件事吗?也许有比我的方法更好的解决方案吗?

你可以毫不费力地在地图中获得给定类型bean的所有实例,因为它是内置的Spring特性

只需自动连接地图,所有这些bean都将被注入,使用bean的ID作为键

@Autowired
Map<String,Exporter> exportersMap;
@Autowired
地图输出者地图;
如果您需要更复杂的东西,例如特定的映射实现或自定义密钥。考虑定义您的自定义出货图,如下:

@Component
class ExporterMap implements Map{
    @Autowired
    private Set<Exporter> availableExporters;
    //your stuff here, including init if required with @PostConstruct
}
@组件
类ExporterMap实现映射{
@自动连线
专用出口设备;
//您的资料在这里,包括init,如果需要@PostConstruct
}

您可以毫不费力地在地图中获得给定类型bean的所有实例,因为它是内置的Spring特性

只需自动连接地图,所有这些bean都将被注入,使用bean的ID作为键

@Autowired
Map<String,Exporter> exportersMap;
@Autowired
地图输出者地图;
如果您需要更复杂的东西,例如特定的映射实现或自定义密钥。考虑定义您的自定义出货图,如下:

@Component
class ExporterMap implements Map{
    @Autowired
    private Set<Exporter> availableExporters;
    //your stuff here, including init if required with @PostConstruct
}
@组件
类ExporterMap实现映射{
@自动连线
专用出口设备;
//您的资料在这里,包括init,如果需要@PostConstruct
}

是您的问题,我如何才能获得所有类型为
Exporter
的bean?你能详细说明一下你在当前示例中遇到的问题吗?是的,我想得到类型导出器的所有实例。问题是,无论我如何尝试获取Bean,Spring都会返回一个包含零条目的映射。因为这是我第一次尝试,我想:在浪费时间之前,最好问问专家(因为这可能是新手犯的错误)。你知道你的
导出器
bean在你的
应用程序上下文中吗?这就是我在这里问的原因。如果我做@Bean。那我的豆子呢?直到现在,我还以为它们被注入到应用程序上下文中。你的问题是,我如何才能获得所有类型为
Exporter
的bean?你能详细说明一下你在当前示例中遇到的问题吗?是的,我想得到类型导出器的所有实例。问题是,无论我如何尝试获取Bean,Spring都会返回一个包含零条目的映射。因为这是我第一次尝试,我想:在浪费时间之前,最好问问专家(因为这可能是新手犯的错误)。你知道你的
导出器
bean在你的
应用程序上下文中吗?这就是我在这里问的原因。如果我做@Bean。那我的豆子呢?直到现在,我还以为它们被注入到了应用程序上下文中。谢谢,工作完美无瑕!其实很简单。我应该自己想出来的。谢谢你,工作完美无瑕!其实很简单。我应该自己解决的。