SpringBoot加載應用事件監(jiān)聽器代碼實例
利用 Spring 工廠加載機制,實例化 ApplicationListener 實現類,并排序對象集合
創(chuàng)建應用事件監(jiān)聽器
創(chuàng)建類實現接口ApplicationListener,可以使用@Order或實現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中配置
# ApplicationListener
org.springframework.context.ApplicationListener=\
com.imooc.diveinspringboot.listener.AfterHelloWorldApplicationListener,\
com.imooc.diveinspringboot.listener.HelloWorldApplicationListener,\
輸出
HelloWorld : application , timestamp : 1591105193644
AfterHelloWorld : application , timestamp : 1591105193644
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaWeb開發(fā)之【Tomcat 環(huán)境配置】MyEclipse+IDEA配置教程
這篇文章主要介紹了JavaWeb開發(fā)之【Tomcat 環(huán)境配置】MyEclipse+IDEA配置教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10JAVA中String類與StringBuffer類的區(qū)別
這篇文章主要為大家詳細介紹了JAVA中String類與StringBuffer類的區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12