Get a reference to currently active dataSource in Spring Boot

40,515

If you have a datasource already created it will be in the spring container, so:

@Autowired
DataSource dataSource;

Should do it.

Share:
40,515

Related videos on Youtube

developer10
Author by

developer10

Updated on July 09, 2022

Comments

  • developer10
    developer10 almost 2 years

    I want to implement db data init via DataSourceInitializer.

    I have these as methods just below my Spring Boot main method, but it seems that it doesn't get executed at all (I tried with intentional removal of characters just to trigger an error which would confirm the execution. Nothing happened.):

    @ConfigurationProperties(prefix="spring.datasource")
    @Bean
    public DataSource getDataSource() {
    
        // i was hoping this was going to pull my current datasource, as 
        // defined in application.properties
        return DataSourceBuilder
                .create()
                .build();
    }
    
    
    @Bean
    public DataSourceInitializer dataSourceInitializer() {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("/data/init/initData.sql"));
    
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    
        // the call to the above method
        dataSourceInitializer.setDataSource(getDataSource());
    
    
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
    
        return dataSourceInitializer;
    }
    

    UPDATE: This question was aimed at getting a reference to the dataSource in use. This question explains how to init the data in a very simple way: DataSourceInitializer is not working on Spring boot 1.2

  • developer10
    developer10 about 7 years
    Did the changes but still no changes in the db. Do you happen to know what else might be wrong with my code?
  • developer10
    developer10 about 7 years
    See update at the end of my question. It works that way, but sure, it's pretty much magic and I aim to learn how to move away from Boot's autoconfiguration. Now, I tried both @Autowireding DataSource as a field, and as you suggested, by placing it below @Bean. Neither way worked.
  • alfcope
    alfcope about 7 years
    @developer10 Is your bean being initialised fine, using the properties? Is the DataSourceInitializer being created? If not, could you show your main method, please?
  • developer10
    developer10 about 7 years
    My main method is a standard SpringBoot's one, just the standard line. I don't know how I can check what you're asking for - I don't see any errors that would suggest it isn't. However, I suspect it's not being called at all. Is that possible? Note: At this moment I'm fine with the default behaviour -- Spring Boot pulls and exectutes the data.sql file which I placed in the root of the resources (as nothing else is necessary for this to work - I just temporarily commented out the method in question).
  • alfcope
    alfcope about 7 years
    Just log something in your @Bean methods. Check also, when starting your app, the bean is not override. Look for something like Overriding bean definition for bean 'dataSourceInitializer'
  • Shane Kenyon
    Shane Kenyon almost 6 years
    Java magic! Thanks for this. In my case I had multiple possible autowire choices so had to specify a more granular datasource class as follows: @Autowired private HikariDataSource dataSource;
  • itro
    itro about 5 years
    what if you have different datasource?
  • Essex Boy
    Essex Boy about 5 years
    @itro you can Autowire any interface or class that's in the Spring container, if there is a unique match it will be found.