Skip to content

Instantly share code, notes, and snippets.

@roshanadh
Created October 21, 2022 13:57
Show Gist options
  • Save roshanadh/8f2756543befe9e0a1d9c385096bd32b to your computer and use it in GitHub Desktop.
Save roshanadh/8f2756543befe9e0a1d9c385096bd32b to your computer and use it in GitHub Desktop.
Bootstrap H2 database with preloaded records
package np.com.roshanadhikary.testdemo.bootstrap;
/**
* Bootstrap the in-memory H2 database with some Battery resources when
* the application starts
*/
@Configuration
public class H2Bootstrap {
private static final Logger logger = LoggerFactory.getLogger(H2Bootstrap.class);
public static final List<Battery> mockBatteries = List.of(
new Battery("Battery Loc 1", String.valueOf(1010), 20500),
new Battery("Battery Loc 2", String.valueOf(1020), 20000),
new Battery("Battery Loc 3", String.valueOf(1040), 30000),
new Battery("Battery Loc 4", String.valueOf(1060), 30500)
);
@Bean
CommandLineRunner initDb(BatteryRepository repository) {
return args -> mockBatteries.forEach(battery ->
logger.info(String.format("Preloading %s", repository.save(battery)))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment