javascript
ServletContextListener在Springboot中的使用
ServletContextListener是servlet容器中的一個API接口, 它用來監(jiān)聽ServletContext的生命周期,也就是相當(dāng)于用來監(jiān)聽Web應(yīng)用的生命周期。今天我們就來說說如何在Springboot 1.5.2這個輕量型框架中如何使用它。?
其實(shí)配置ServletContextListener與其它Filter, Listener, Servlet方法是一致的,具體可參考Springboot 1.5.2?官方文檔?
首先寫一個類來實(shí)現(xiàn)ServletContextListener接口,并實(shí)現(xiàn)contextInitialized(), contextDestroyed()兩個父類方法,并使用@WebListener注解, 具體代碼如下:
其次在Springboot web 應(yīng)用啟動代碼中添加@ServletComponentScan注解,使我們的Springboot應(yīng)用在啟動時能掃描到該Listener.
運(yùn)行項(xiàng)目,我們可以springboot的啟動log看到如下log信息,即表明我們的ServletContextListener注冊成功。
2017-03-28 20:31:43.274 INFO 8808 --- [ost-startStop-1] org.liting.PmsServletContextListener : liting: contextInitialized那么我們就可以利用ServletContextListener來監(jiān)聽我們的web應(yīng)用及做一些對應(yīng)的業(yè)務(wù)邏輯。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的ServletContextListener在Springboot中的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: el-table表格fixed=“rig
- 下一篇: Spring原理之代理与动态代理模式总结