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應用及做一些對應的業務邏輯。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/280215.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280215.shtml
英文地址,請注明出處:http://en.pswp.cn/news/280215.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《ASP.NET Core 6框架揭秘》實例演示[34]:緩存整個響應內容

我們利用ASP.NET開發的大部分API都是為了對外提供資源,對于不易變化的資源內容,針對某個維度對其實施緩存可以很好地提供應用的性能。《內存緩存與分布式緩存的使用》介紹的兩種緩存框架(本地內存緩存和分布式緩存)為我們提供了簡…

常見端口介紹

Win常用端口 TCP端口(靜態端口)TCP 0 ReservedTCP 1TCP Port Service MultiplexerTCP 2DeathTCP 5Remote Job Entry,yoyoTCP 7EchoTCP 11SkunTCP 12BomberTCP 16SkunTCP 17SkunTCP 18消息傳輸協議,skunTCP 19SkunTCP 20FTP Data,Amanda TCP 2…

如何更改Windows 10鎖定屏幕超時

By default, Windows 10’s lock screen times out and switches off your monitor after one minute. If you’d like it to stick around longer than that–say, if you have background picture you like looking at or you enjoy having Cortana handy–there’s a simple…

ios 開發賬號 退出協作_如何在iOS 10中的Notes上進行協作

ios 開發賬號 退出協作iOS’ Notes app provides a convenient way to remember the great ideas you come up with and all the things you have to do. The app has evolved over the years, and iOS 10 adds even more features–including collaboration. iOS的Notes應用程…

poj 1182

食物鏈Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 86494 Accepted: 25887Description 動物王國中有三類動物A,B,C,這三類動物的食物鏈構成了有趣的環形。A吃B, B吃C,C吃A。 現有N個動物,以1-N編號。每…

條款6:若不想使用編譯器自動生成的函數,就該明確拒絕

如果自己定義的類中并不需要copy assignment操作符或者copy構造函數,為了避免編譯器自動生成因為編譯器自動生成的沒什么用,一般是按照順序進行賦值或者拷貝,對于有對象內含有指針的話可能會出現一些問題可以在private中聲明(并不…

為什么Android Geeks購買Nexus設備

The Galaxy S III is the highest-selling Android phone, but much of the geeky buzz is around the Nexus 4 – and the Galaxy Nexus before it. Nexus devices are special because they don’t have some of Android’s biggest problems. Galaxy S III是最暢銷的Android…

你的知識死角不能否定你的技術能力

有些事情你不知道,但你一定能解決。 有些人通過我賬號資料里的微信加我,然后問我一些所謂“怎么辦”的問題,不是我不告訴你,而是我確實不知道。我確實有很高的title,也確實有很多的技術積累,但我并沒有達到…

算法練習(十二)

The Suspects Description 嚴重急性呼吸系統綜合癥( SARS), 一種原因不明的非典型性肺炎,從2003年3月中旬開始被認為是全球威脅。為了減少傳播給別人的機會, 最好的策略是隔離可能的患者。 在Not-Spreading-Your-Sickness大學( NSYSU), 有許多學生團體。同一組的學生經常彼此相…

day4----函數-閉包-裝飾器

day4----函數-閉包-裝飾器 本文檔內容: 1 python中三種名稱空間和作用域 2 函數的使用 3 閉包 4 裝飾器 一 python中三種名稱空間和作用域 1.1名稱空間: 當程序運行時,代碼從上至下依次執行,它會將變量與值得關系存儲在一個空間中…

濾波器和均衡器有什么區別_什么是均衡器,它如何工作?

濾波器和均衡器有什么區別It’s in your car, home theater system, phone, and audio player but it doesn’t have an instruction manual. It’s an equalizer, and with a little know-how you can tweak your audio and fall in love with it all over again. 它在您的汽車…

網絡視頻監控與人臉識別

明天又要去面試了,趁次機會也將以前做的東西總結一下,為以后理解提供方便,也再加深下印象。 網絡視頻監控與人臉識別主要由三個程序組成:1、視頻采集與傳輸程序;2、接受與顯示程序;3、人臉識別程序。下面就…

esxi.主機配置上聯端口_為什么現代的電腦機箱仍然具有USB 2.0端口?

esxi.主機配置上聯端口With USB 3.0 becoming more prevalent with each passing year now, you may have found yourself wondering why modern computers still have USB 2.0 ports built into them. With that in mind, today’s SuperUser Q&A post has the answers to…

使用命令導入、導出mysql數據

1.導出全部數據庫 利用mysqldump的—all-databases參數可以一口氣把你數據庫root用戶下的所有數據庫一口氣導出到一個sql文件里。然后,重裝系統后使用source命令可以再一口氣倒回來。 需要確定mysql安裝的路徑:本機是:C:\Program Files\MySQL…

面試--跨域--cors

cors是什么 cors 跨域資源共享 Cross-origin resource sharing是一種跨域的解決方案 它允許瀏覽器向跨源服務器,發出XMLHttpRequest請求,從而克服了AJAX只能同源使用的限制。 但是需要瀏覽器的支持。值得注意的是:整個CORS通信過程&#xff0…

【原理圖操作】原理圖更新PCB時未改動元器件布局變動問題?

轉載PCB布局、布線完工之后,由于設計功能,發現不完善時, 原理圖部分功能需要改動,再改原理圖,修改完成后,導入PCB過程中,發現PCB中未改動(部分)的元器件 布局發生了變化,…

關閉edge任務欄預覽_如何在Microsoft Edge中關閉選項卡預覽

關閉edge任務欄預覽Now that it has extension support, Microsoft Edge is becoming a more and more viable browser. One feature people seem to either love or hate is the pop-up preview you get when you hover over a tab. There’s no built-in setting that lets y…

oracle 創建view時,授權給用戶

解決方法: 以dba用戶登錄 sqlplus / as sysdba 賦予scott用戶創建VIEW的權限 grant create view to scott 以scott用戶登錄oracle conn scott/tiger 創建視圖成功 CREATE OR REPLACE VIEW myview AS 轉載于:https://www.cnblogs.com/523823-wu/p/7635436.html

[BZOJ 1072] 排列perm

Link&#xff1a; BZOJ 1072 傳送門 Solution&#xff1a; 一道直接next_permutation純暴力就能過的題&#xff1f; 難道2007年時大家都不知道next_permutation這個函數嗎 還是用復雜度更優的狀壓DP吧 設$dp[i][j]$為狀態為$i$且對$d$余$j$的個數&#xff0c; 注意$dp[(1<&l…

智能手機丟失 數據安全_丟失智能手機時該怎么辦

智能手機丟失 數據安全Phones get stolen or lost everyday. With a plethora of data ripe for identity-theft on it, a lost phone can easily make your blood run cold. Take a deep breath, How-To Geek will talk you through this. 手機每天都會被盜或丟失。 隨著大量用…