日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

ServletContextListener在Springboot中的使用

發布時間:2023/12/9 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ServletContextListener在Springboot中的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ServletContextListener是servlet容器中的一個API接口, 它用來監聽ServletContext的生命周期,也就是相當于用來監聽Web應用的生命周期。今天我們就來說說如何在Springboot 1.5.2這個輕量型框架中如何使用它。?
其實配置ServletContextListener與其它Filter, Listener, Servlet方法是一致的,具體可參考Springboot 1.5.2?官方文檔?
首先寫一個類來實現ServletContextListener接口,并實現contextInitialized(), contextDestroyed()兩個父類方法,并使用@WebListener注解, 具體代碼如下:

package org.liting;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;import org.slf4j.Logger; import org.slf4j.LoggerFactory; @WebListener public class PmsServletContextListener implements ServletContextListener{ private Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub logger.info("liting: contextDestroyed"); } @Override public void contextInitialized(ServletContextEvent arg0) { // TODO Auto-generated method stub logger.info("liting: contextInitialized"); } }

其次在Springboot web 應用啟動代碼中添加@ServletComponentScan注解,使我們的Springboot應用在啟動時能掃描到該Listener.

運行項目,我們可以springboot的啟動log看到如下log信息,即表明我們的ServletContextListener注冊成功。

2017-03-28 20:31:43.274 INFO 8808 --- [ost-startStop-1] org.liting.PmsServletContextListener : liting: contextInitialized

那么我們就可以利用ServletContextListener來監聽我們的web應用及做一些對應的業務邏輯。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的ServletContextListener在Springboot中的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。