After the start of the project Springboot perform some custom code

Springboot provides us with two "boot" approach of some methods: ApplicationRunner and CommandLineRunner.  

The purpose of these two methods is provided in order to meet, to perform some of the methods when the project starts immediately. We can achieve ApplicationRunner and CommandLineRunner, to achieve, they are after SpringApplication execution begins execution.  

CommandLineRunner interface can be used command line parameters received string array, ApplicationRunner is used for receiving the parameters ApplicationArguments

The sample code

@ Component // spring containers are managed   
@Order (1) // If more custom ApplicationRunner, used to identify the order of execution   
public class MyApplicationRunner the implements ApplicationRunner {   
    @Override   
    public void RUN (ApplicationArguments applicationArguments) throws Exception {   
        System.out.println ( "-------------->" + "program start, now =" + new new a Date ());   
        the myTimer ();   
    }   
  
    public static void the myTimer () {   
        the Timer Timer new new = the Timer ();   
        timer.schedule (the TimerTask new new () {   
            @Override   
            public void RUN () {   
                System.out.println ( "------ -------- scheduled task");   
            }   
        }, 0 , 1000);  
    }  
}

  Results of the

2018-02-08 14:10:16.490  INFO 10236 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)  

--------------> project started, now = Thu Feb  08  14: 10: 16 CST  2018  

Timing task ------ --------   2018- 02- 08  14: 10: 16.497 the INFO  10236 --- [main] com.mlxs.springboot01.web.MainApp: MainApp Started in  5.595 seconds The ( running the JVM  for  6.334)  

-------- ------ regular tasks  

-------- ------ regular tasks  

-------- ------ regular tasks  

-------- ------ regular tasks  

-------- ------ regular tasks  

-------- ------ regular tasks  

Guess you like

Origin www.cnblogs.com/DreamFather/p/11327396.html