詳解springboot整合Listener的兩種方式
1.通過注解
編寫啟動類
package cn.bl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
編寫一個監(jiān)聽器
package cn.bl.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class FirstListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("init .. "); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("desroyed .. "); } }
當(dāng)執(zhí)行App的時候
2.通過函數(shù)
package cn.bl.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class SecondListener implements ServletContextListener{ @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("second servletListener init .. "); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("second servletListener destroy .. "); } } package cn.bl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.context.annotation.Bean; import cn.bl.listener.SecondListener; @SpringBootApplication public class App2 { public static void main(String[] args) { SpringApplication.run(App2.class, args); } @Bean public ServletListenerRegistrationBean<SecondListener>getBean(){ ServletListenerRegistrationBean<SecondListener>bean = new ServletListenerRegistrationBean<>(new SecondListener()); return bean; } }
總結(jié)
以上所述是小編給大家介紹的springboot整合Listener的兩種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring?Boot?Nacos?實現(xiàn)不停服發(fā)布過程詳解
這篇文章主要為大家介紹了Spring?Boot?Nacos實現(xiàn)不停服發(fā)布過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Java如何導(dǎo)出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾
這篇文章主要介紹了Java導(dǎo)出數(shù)據(jù)庫中的所有數(shù)據(jù)表到指定文件夾,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問題,方法重載,方法簽名)
這篇文章主要介紹了Java?方法(方法的定義,可變參數(shù),參數(shù)的傳遞問題,方法重載,方法簽名),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09java字符串的替換replace、replaceAll、replaceFirst的區(qū)別說明
這篇文章主要介紹了java字符串的替換replace、replaceAll、replaceFirst的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java異常處理運行時異常(RuntimeException)詳解及實例
這篇文章主要介紹了 Java異常處理運行時異常(RuntimeException)詳解及實例的相關(guān)資料,需要的朋友可以參考下http://time.qq.com/?pgv_ref=aiotime2017-05-05