How to get all self injected Beans of a special type?

575 views Asked by At

I would like to build a Spring application, where new components can be added easily and without much configuration. For example: You have different kinds of documents. These documents should be able to get exported into different fileformats.

To make this functionality easy to maintain, it should (basically) work the following way:

  • Someone programs the file format exporter
  • He/ She writes a component, which checks if the file format exporter is licensed (based on Spring Conditions). If the exporter is licensed a specialized Bean is injected in the application context.
  • The "whole rest" works dynamically based on the injected beans. Nothing needs to be touched in order to display it on the GUI, etc.

I pictured it the following way:

@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;
    }
}

In order to work with those exporters (display them, etc.) I need to get all of them. I tried this:

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

Unfortunate this does not work (0 beans returned). I am fairly new to this, would anyone mind to tell me how this is properly done in Spring? Maybe there is a better solution for my problem than my approach?

1

There are 1 answers

1
José Carlos On BEST ANSWER

You can get all instances of a given type of bean in a Map effortlessly, since it's a built in Spring feature.

Simply autowire your map, and all those beans will be injected, using as a key the ID of the bean.

@Autowired
Map<String,Exporter> exportersMap;

If you need something more sophisticated, such as a specific Map implementation or a custom key. Consider defining your custom ExporterMap, as follows

@Component
class ExporterMap implements Map{
    @Autowired
    private Set<Exporter> availableExporters;
    //your stuff here, including init if required with @PostConstruct
}