Springboot provides us with two ways to "boot" some methods: ApplicationRunner and CommandLineRunner.
The purpose of these two methods is to satisfy the purpose of executing certain methods immediately upon project startup. We can implement it by implementing ApplicationRunner and CommandLineRunner, both of which start executing after SpringApplication is executed.
The CommandLineRunner interface can be used to receive command line parameters of string arrays. ApplicationRunner uses ApplicationArguments to receive parameters, which seems to be more awesome.
Let's take a look at CommandLineRunner:
package com.springboot.study;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;/** * Created by pangkunkun on 2017/9/3. */@Componentpublic class MyCommandLineRunner implements CommandLineRunner{ @Override public void run(String... var1) throws Exception{ System.out.println("This will be executed when the project was started!"); }}ApplicationRunner:
package com.springboot.study;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.stereotype.Component;/** * Created by pangkunkun on 2017/9/3. */@Componentpublic class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments var1) throws Exception{ System.out.println("MyApplicationRunner class will be executed when the project was started!"); }}The implementation of both methods is very simple, just implement the corresponding interface directly. Remember to add @Component annotation to the class.
If you want to specify the order in which the startup method is executed, you can implement the org.springframework.core.Ordered interface or use the org.springframework.core.annotation.Order annotation.
Here we use ApplicationRunner as an example to implement it separately.
Ordered interface:
package com.springboot.study;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.Ordered;import org.springframework.steretype.Component;/** * Created by pangkunkun on 2017/9/3. */@Componentpublic class MyApplicationRunner implements ApplicationRunner,Ordered{ @Override public int getOrder(){ return 1;//Know the specified order by setting the numbers here} @Override public void run(ApplicationArguments var1) throws Exception{ System.out.println("MyApplicationRunner1!"); }}Order annotation implementation method:
package com.springboot.study;import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.core.Ordered;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;/** * Created by pangkunkun on 2017/9/3. * Here we specify the execution order by setting the value of the value*/@Component@Order(value = 1)public class MyApplicationRunner implements ApplicationRunner{ @Override public void run(ApplicationArguments var1) throws Exception{ System.out.println("MyApplicationRunner1!"); }} No other comparison methods are listed here, just implement them yourself.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.