SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實例
利用 Spring 工廠加載機制,實例化 ApplicationListener 實現(xiàn)類,并排序?qū)ο蠹?/p>
創(chuàng)建應(yīng)用事件監(jiān)聽器
創(chuàng)建類實現(xiàn)接口ApplicationListener,可以使用@Order或?qū)崿F(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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用Python webdriver圖書館搶座自動預(yù)約的正確方法2. Android Studio實現(xiàn)格式化XML代碼順序3. Linux刪除系統(tǒng)自帶版本Python過程詳解4. Python 合并拼接字符串的方法5. PHP如何開啟Opcache功能提升程序處理效率6. 在線php代碼縮進、代碼美化工具:PHP Formatter7. Python3 json模塊之編碼解碼方法講解8. Android 簡單的實現(xiàn)滑塊拼圖驗證碼功能9. ASP.NET MVC使用jQuery ui的progressbar實現(xiàn)進度條10. 淺談由position屬性引申的css進階討論

網(wǎng)公網(wǎng)安備