java - What does CommandLineRunner do in this case? -


i'm new spring & not experienced in java admittedly. trying go through building rest services spring tutorial on spring.io website. came across following code segment , i'm confused does.

@configuration @componentscan @enableautoconfiguration public class application {      @bean     commandlinerunner init(accountrepository accountrepository,             bookmarkrepository bookmarkrepository) {         return (evt) -> arrays.aslist(                 "jhoeller,dsyer,pwebb,ogierke,rwinch,mfisher,mpollack,jlong".split(","))                 .foreach(                         -> {                             account account = accountrepository.save(new account(a,                                     "password"));                             bookmarkrepository.save(new bookmark(account,                                     "http://bookmark.com/1/" + a, "a description"));                             bookmarkrepository.save(new bookmark(account,                                     "http://bookmark.com/2/" + a, "a description"));                         });     }      public static void main(string[] args) {         springapplication.run(application.class, args);     } } 

i looked commandlinerunner on spring boot docs, , says it's interface gets implemented if want execute code when application starts. limited knowledge, commandlinerunner not getting implemented in above code segment. also, have no clue init() method came or is.

additionally, downloaded complete code github repo i'm not quite sure how run it. read earlier today springapplication.run() makes don't have deploy external service tomcat. when tried java -jar ./jarfilename application class (seemed natural choice since had main() ), gave error.

any appreciated.

to answer first question:

commandlinerunner getting implemented lambda expression:

return (evt) -> arrays.aslist(... 

second thing, init() method spring bean definition, indicated @bean annotation.

and third, way of running application got right. java -jar jarfile should run it. looked @ modules in git repo , of modules missing spring-boot-maven-plugin configuration makes jars executable. can run them command line this: mvn spring-boot:run


Comments