In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 211
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.