CommandLineRunner je funkční rozhraní s metodou void run(String... args) throws Exception. V této metodě můžeme uvést vše, co chceme aby se stalo a startu aplikace. Jednou z možností jak vytvořit CommandLineRunner je vlastní třída s anotací @Configuration a následně metoda s anotací @Bean vracející CommandLineRunner
@Configuration
class AppConfig {
@Bean
fun init(bookRepository: BookRepository): CommandLineRunner {
return CommandLineRunner {
bookRepository.save(BookEntity(title = "Cyber Leviathan"))
bookRepository.save(BookEntity(title = "Last Hunt"))
}
}
}
Jiný způsob je extendovat CommandLineRunner a implementovat metodu run()
@SpringBootApplication
class SpringData2Application : CommandLineRunner {
override fun run(vararg args: String?) {
println("Running ...")
}
}
fun main(args: Array<String>) {
runApplication<SpringData2Application>(*args)
}