Monday, March 14, 2016

Java Config dataSourceInitializer

@Value("classpath:db/sql/insert-data.sql")
private org.springframework.core.io.Resource DATA_SCRIPT;

@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {

   final DataSourceInitializer initializer = new DataSourceInitializer();
   initializer.setDataSource(dataSource);
   initializer.setDatabasePopulator(databasePopulator());  
   return initializer;
}

private DatabasePopulator databasePopulator() {
   final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
   populator.addScript(DATA_SCRIPT);
   return populator;
}

OR

populator.addScript(new ClassPathResource("db/sql/insert-data.sql"));

No comments: