SpringBoot run code on startup

In the spring  the actual development boot project, we sometimes need to load some data item or service to start in advance to complete certain actions. To solve this problem, Spring boot provides us with a method: to achieve this demand by implementing the interface CommandLineRunner.

Implementation: only one class without the need of additional configuration. 

Implementation steps:

1. Create a class that implements the interface CommandLineRunner of MyStartupRunnerTest

  1. package com.energy;  
  2.   
  3. import org.springframework.boot.CommandLineRunner;  
  4. import org.springframework.core.annotation.Order;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. /** 
  8.  * Created by CavanLiu on 2017/2/28 0028. 
  9.  */  
  10. @Component  
  11. @Order(value=1)
  12. public class MyStartupRunnerTest implements CommandLineRunner  
  13. {  
  14.     @Override  
  15.     public void run(String... args) throws Exception  
  16.     {  
  17.         System.out.println(">>>>This is MyStartupRunnerTest Order=1. Only testing CommandLineRunner...<<<<");  
  18.     }  
  19. }  

Released five original articles · won praise 0 · Views 5054

Guess you like

Origin blog.csdn.net/ab1991823/article/details/104840889