SpringBoot container startup execution method and container shutdown execution method

Start the execution method:

Implement the run method in the ApplicationRunner interface .

If there are multiple implementation classes and you need them to be executed in a certain order, you can add the @Order annotation to the implementation class. @Order(value=integer value). SpringBoot will execute in order from small to large according to the value in @Order.


close execution method

One is to implement the DisposableBean interface, and the other is to use the @PreDestroy annotation.

  1. @Component  
  2. publicclass TestImplDisposableBean implements DisposableBean, ExitCodeGenerator {   
  3.   
  4.     @Override  
  5.     publicvoid destroy() throws Exception {   
  6.   
  7.         System.out.println("<<<<<<<<<<<我被销毁了......................>>>>>>>>>>>>>>>");  
  8.         System.out.println("<<<<<<<<<<<我被销毁了......................>>>>>>>>>>>>>>>");  
  9.     }  
  10.   
  11.     @Override  
  12.     public int  getExitCode() {   
  13.   
  14.         return5;   
  15.     }  
  16. }  

  1. @Component  
  2. public class  TestAnnotationPreDestroy {   
  3.   
  4.     @PreDestroy  
  5.     publicvoid destory() {   
  6.   
  7.         System.out.println( "I was destroyed,,,,,I used @PreDestory method,,,,,,," );  
  8.         System.out.println( "I was destroyed,,,,,I used @PreDestory method,,,,,,," );  
  9.     }  
  10. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324772241&siteId=291194637