Spring when the project started, ApplicationRunner and CommandLineRunner usage

Spring when the project started, if you need to initialize resources, can achieve ApplicationRunner or CommandLineRunner, and can use annotation @Order specify their running order.


Sample Code: https://github.com/rickding/HelloJava/tree/master/HelloRunner

3 types of documents:

image.png


RunnerApplication.java achieve SpringBootApplication main program:

image.png


HelloApplicationRunner.java achieve ApplicationRunner:

image.png


HelloCommandLineRunner.java achieve CommandLineRunner, and specifies @Order (1):

image.png


Output log:

image.png


Reference article: https://blog.csdn.net/qq_28804275/article/details/80891941

Guess you like

Origin blog.51cto.com/13851865/2466231