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. SSM框架JSP使用Layui實現(xiàn)layer彈出層效果2. IntelliJ IDEA導(dǎo)入jar包的方法3. 刪除docker里建立容器的操作方法4. IntelliJ IDEA導(dǎo)出項目的方法5. java使用xfire搭建webservice服務(wù)的過程詳解6. .Net中的Http請求調(diào)用詳解(Post與Get)7. JS如何在數(shù)組指定位置插入元素8. Django實現(xiàn)將views.py中的數(shù)據(jù)傳遞到前端html頁面,并展示9. PHP下對緩沖區(qū)的控制10. Java源碼解析之ClassLoader
