Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the two ways for SpringBoot to execute custom methods after the project starts?

Shulou Source: shulou.com Published: 2022-06-03 10:20:17 10月01日 Update

SpringBoot in the project after the start of the two ways to implement custom methods are, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Two ways to execute custom methods after SpringBoot project startup

When testing the configuration of the configuration center, you want to print the configuration items after the project starts successfully, and then you need to execute a custom class

In general, some operations to initialize data will also be carried out in this place.

The first way to implement CommandLineRunner interface

Customize the class and implement the CommandLineRunner interface, implement the run () method, and put the statements that need to be executed in the run () method

Example:

@ Component@Order (1) / / the smaller the order in which the control class executes, the earlier the public class StartInitializer implements CommandLineRunner {@ Override public void run (String...) Args) throws Exception {System.out.println ("the project starts, executes the method of the CommandLineRunner implementation class"); the second way to implement the ApplicationRunner interface

Customize the class and implement the ApplicationRunner interface, implement the run () method, and put the statements that need to be executed in the run () method

Example:

@ Component@Order (2) / / the smaller the order in which the control class executes, the earlier the public class AppInitializer implements ApplicationRunner {@ Override public void run (ApplicationArguments args) throws Exception {System.out.println ("the project starts, executes the method of the ApplicationRunner implementation class");}} the difference between the two

The difference is that the parameter types in the implementation method run () are different.

The parameter type of the run () method that implements the ApplicationRunner interface is: ApplicationArguments

The parameter type of the run () method that implements the CommandLineRunner interface is: String...

Realize the effect

Execute some custom code after the Springboot project starts

Springboot provides us with two ways to "boot up" some methods: ApplicationRunner and CommandLineRunner.

The purpose of these two methods is to satisfy that some methods are executed as soon as the project starts. We can do this by implementing ApplicationRunner and CommandLineRunner, both of which start after the SpringApplication is executed.

The CommandLineRunner interface can be used to receive command line arguments to an array of strings, and ApplicationRunner is used to receive parameters using ApplicationArguments.

Code example

@ Component// is managed by spring container @ Order (1) / / if there are multiple custom ApplicationRunner, it is used to indicate the execution order public class MyApplicationRunner implements ApplicationRunner {@ Override public void run (ApplicationArguments applicationArguments) throws Exception {System.out.println ("- >" + "project startup, now=" + new Date ()); myTimer ();} public static void myTimer () {Timer timer = new Timer () Timer.schedule (new TimerTask () {@ Override public void run () {System.out.println ("- scheduled task -");}}, 0, 1000);}}

Execution result

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

-> Project launch, now=Thu Feb 08 14:10:16 CST 2018

-scheduled tasks-

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

-scheduled tasks-

-scheduled tasks-

-scheduled tasks-

-scheduled tasks-

-scheduled tasks-

-scheduled tasks-

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Methods projects tasks interfaces methods parameters types order configuration code statements Xiaoyue help control clarity success content can be passed commands places Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Microsoft Linux Shulou Tech Info