Search code examples
springspring-bootspring-dataspring-cloud

How can I list all Spring Boot datasources?


How can I list all Spring Boot datasources? Including ones that configured with Spring Cloud Config and others that autoconfigured being based on Maven dependencies of db drivers in pom.xml


Solution

  • Assuming you have multiple datasources with different qualifiers , you can get it in a list. In fact not just for Datasource type , to list of any type , you can have similar code. and this list will have all bean implementations.

    @Autowired
    List<DataSource> datasources;
    

    Or you can access by bean name as well.

    @Autowired
    Map<String,DataSource> datasources;