spring項目啓動/關閉執行(觸發)事件

*項目啓動觸發

1.實現接口CommandLineRunner,配置註解@Component @Order(執行的順序) 可添加多個事件

 

2.ApplicationRunner

 

3.ApplicationListener<ContextRefreshedEvent>

 

*項目停止觸發

1.ApplicationListener<ContextClosedEvent>

 

demo:

/**
 * 應用項目啓動-執行事件
 */
@Component
@Order(1)
public class RocketConsumerListener implements CommandLineRunner {

   @Autowired
   private MsgConsumer msgConsumer;

   /**
    * RocketMQ監聽器
    *
    * 啓動消費者
    *
    * @param args
    * @throws Exception
    */
   @Override
   public void run(String... args) throws Exception {
      System.out.println("RocketMQ消費者啓動----");
      msgConsumer.init();
   }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章