Search code examples
javaspringspring-bootspring-data

Make Spring Data automatic DataSource dependent on Bean


I have a Bean that fetches database credentials from an external source, and sets them as system properties. I want to ensure the DataSource automatically configured by Spring Data depends on my Bean, so I can ensure that the credentials have been fetched and set before the DataSource Bean is created.

Is there a way to do this without implementing a DataSource bean myself with the DependsOn annotation?


Solution

  • It may be potential solution for your reference , you can make use of spring @Configuration and @AutoConfigureBefore the code snippet like following

    @AutoConfiguration(before = { DataSourceAutoConfiguration.class, SqlInitializationAutoConfiguration.class }) 
    // above is AutoConfiguration is dataSource Autowired , such as mysql oracle ,and some else
    public class CustomDataSourceAutoConfiguration {
    
        @Bean
         public CustomCredentials customCredentials(){
             // get your external source credentials
         }
    
    }
    
    
    

    for more annotation, you can read the spring source code , tht package path is org.springframework.boot.autoconfigure.condition