Event actions when spring boot starts and closes:

Posted by Scarum on Tue, 14 May 2019 14:55:51 +0200

Click back to the directory

This chapter is not too important, but you need to know that there is this thing.

Execution at destruction:

Inherit from Disposable Bean and register it as a bean.

import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
/**
 * Execution at the end
 * @author dmw
 *
 * 2019 April 15th 2013
 */
@Component
public class MyDisposableBean implements DisposableBean{

	@Override
	public void destroy() throws Exception {
		System.out.println("End");
		
	}

}

Post-startup execution:

There are two ways:

1. Implementation from Application Runner

@Component
public class MyApplicationRunner1 implements ApplicationRunner{

	@Override
	public void run(ApplicationArguments args) throws Exception {
		
	}

}

2. Implementation from CommandLine Runner

@Component
public class MyCommandLineRunner1 implements CommandLineRunner{

	@Override
	public void run(String... args) throws Exception {
		
	}	
	
}

They are very similar, except that the parameters of Application Runner are spring parameters and CommandLine Runner are command line parameters. If you don't have any special requirements, you can use either one.

Processing of execution order:

Whether it's destruction or start-up, sometimes we want them to be in the order we want them to be.

A comment @Order is needed here.

As follows:

@Order(0)
@Component
public class MyCommandLineRunner1 implements CommandLineRunner{...

@Order(1)
@Component
public class MyApplicationRunner1 implements ApplicationRunner{...

@Order(-1)
@Component
public class MyApplicationRunner2 implements ApplicationRunner{...

 Execution order: MyApplicationRunner2 > MyCommandLineRunner1 > MyApplicationRunner1 

@ The smallest value in Order is Integer.min and the largest is Integr.max. The smaller the order, the higher the order.

Similarly, if there are multiple destruction events, you can add @Order if you want to order them.

Other startup events:

There are two kinds of start-up events: one is to listen for events during spring boot start-up, the other is to execute events immediately after spring boot start-up is successful.

What we just talked about is the implementation after successful start-up.

Now let's talk about the listening events in the startup process, which you don't need, but you can understand.

There are six events in the start-up process as follows:

Application Starting Event is the start of a run, but is sent before any processing, except for audiences and initial registration.
Application Environment Prepared Event is used when sent to a known context, but before the context is created.
The Application PreparedEvent refresh started just before it was launched, but the subsequent bean definitions have been loaded.
The Application Started Event context has been refreshed and sent, but any application and command line runner-up has been invoked before.
Application ReadyEvent is sent when any application and command line runner-up is called. It indicates that the application is ready to service the request.
Application Failed Event is sent abnormally at startup.

How to use it?

Write an Application Starting Event event

public class MyApplicationStartingEvent implements ApplicationListener<ApplicationStartingEvent>{

	@Override
	public void onApplicationEvent(ApplicationStartingEvent event) {
		System.out.println("=================start-up========");
	}

}

Then register in the application Class.

@SpringBootApplication
public class App {
	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(App.class);
		application.addListeners(new MyApplicationStartingEvent());
		application.run(args);
	}
}

 

Topics: Spring