SpringBoot加載應用事件監(jiān)聽器代碼實例
利用 Spring 工廠加載機制,實例化 ApplicationListener 實現(xiàn)類,并排序對象集合
創(chuàng)建應用事件監(jiān)聽器
創(chuàng)建類實現(xiàn)接口ApplicationListener,可以使用@Order或實現(xiàn)Orderd接口進行排序
@Order(Ordered.HIGHEST_PRECEDENCE)public class HelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println('HelloWorld : ' + event.getApplicationContext().getId()+ ' , timestamp : ' + event.getTimestamp()); }}
public class AfterHelloWorldApplicationListener implements ApplicationListener<ContextRefreshedEvent>,Ordered { @Override public void onApplicationEvent(ContextRefreshedEvent event) { System.out.println('AfterHelloWorld : ' + event.getApplicationContext().getId()+ ' , timestamp : ' + event.getTimestamp()); } @Override public int getOrder() { return Ordered.LOWEST_PRECEDENCE; }}
在spring.properties中配置
# ApplicationListenerorg.springframework.context.ApplicationListener=com.imooc.diveinspringboot.listener.AfterHelloWorldApplicationListener,com.imooc.diveinspringboot.listener.HelloWorldApplicationListener,
輸出
HelloWorld : application , timestamp : 1591105193644AfterHelloWorld : application , timestamp : 1591105193644
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 使用Python和百度語音識別生成視頻字幕的實現(xiàn)2. 利用ajax+php實現(xiàn)商品價格計算3. CSS可以做的幾個令你嘆為觀止的實例分享4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. xml中的空格之完全解說6. ASP刪除img標簽的style屬性只保留src的正則函數(shù)7. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法8. axios和ajax的區(qū)別點總結9. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?10. css代碼優(yōu)化的12個技巧
