Springboot使用@WebListener?作為web監(jiān)聽器的過程解析
一、使用@WebListener 作為web監(jiān)聽器
1、使用監(jiān)聽器必須在啟動(dòng)類上添加掃描
@ServletComponentScan
@ServletComponentScan @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
二、Listener的四種注冊(cè)方式
1、@WebListener 注解方式
示例1:Session監(jiān)聽器
- @WebListener 本質(zhì)上是事件監(jiān)聽
- HttpSessionListener和HttpSessionAttributeListener都是實(shí)現(xiàn)了EventListener接口
@WebListener public class SessionListener implements HttpSessionAttributeListener, HttpSessionListener { // ... ... 省略實(shí)現(xiàn)方法 }
示例2:屬性監(jiān)聽器
@WebListener public class ContextListener implements ServletContextAttributeListener,ServletContextListener { // ... ... 省略實(shí)現(xiàn)方法 }
2、普通Bean方式
1、創(chuàng)建監(jiān)聽器類
將 Listener 當(dāng)成一個(gè)普通的 spring bean,spring boot 會(huì)自動(dòng)將其包裝為 ServletListenerRegistrationBean
對(duì)象
- ServletContextListener 需要實(shí)現(xiàn)該接口
- contextInitialized 容器初始化接口
- contextDestroyed 容器銷毀方法
@Component public class BeanContextListener implements ServletContextListener { // ... ... 省略實(shí)現(xiàn)方法 }
3、Bean手動(dòng)注冊(cè)方式
1、創(chuàng)建一個(gè)配置監(jiān)聽類
ServletContextListener 主動(dòng)注入
public class ConfigContextListener implements ServletContextListener { // ... .... 省略實(shí)現(xiàn)方法 }
2、通過配置監(jiān)聽類將自定義監(jiān)聽類注冊(cè)
@Bean public ServletListenerRegistrationBean configContextListener() { ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); bean.setListener(new ConfigContextListener()); return bean; }
4、上下文初始化方式
在初始化類中注入監(jiān)聽器
1、創(chuàng)建監(jiān)聽器
public class SelfContextListener implements ServletContextListener { // ... ... 省略實(shí)現(xiàn)方法 }
2、 使用上下文添加監(jiān)聽器
@Component public class ExtendServletConfigInitializer implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addListener(SelfContextListener.class); } }
到此這篇關(guān)于Springboot使用@WebListener 作為web監(jiān)聽器的文章就介紹到這了,更多相關(guān)Springboot 使用@WebListener內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用異或運(yùn)算實(shí)現(xiàn)簡單的加密解密算法實(shí)例代碼
這篇文章主要介紹了Java使用異或運(yùn)算實(shí)現(xiàn)簡單的加密解密算法實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12詳解在Java程序中運(yùn)用Redis緩存對(duì)象的方法
這篇文章主要介紹了在Java程序中運(yùn)用Redis緩存對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn)
這篇文章主要介紹了maven繼承父工程統(tǒng)一版本號(hào)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08SpringBoot2零基礎(chǔ)到精通之JUnit 5與指標(biāo)監(jiān)控
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)JUnit 5與指標(biāo)監(jiān)控2022-03-03簡述springboot及springboot cloud環(huán)境搭建
這篇文章主要介紹了簡述springboot及springboot cloud環(huán)境搭建的方法,包括spring boot 基礎(chǔ)應(yīng)用環(huán)境搭建,需要的朋友可以參考下2017-07-07利用Mybatis向PostgreSQL中插入并查詢JSON字段
這篇文章主要介紹了利用Mybatis向PostgreSQL中插入并查詢JSON字段,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07解決Eclipse發(fā)布到Tomcat丟失依賴jar包的問題
這篇文章介紹了如何在Eclipse中配置部署裝配功能,以確保在將Web項(xiàng)目發(fā)布到Tomcat服務(wù)器時(shí)不會(huì)丟失任何依賴jar包,通過手動(dòng)配置或使用構(gòu)建工具腳本,可以自動(dòng)化這個(gè)過程,提高開發(fā)效率和應(yīng)用程序的穩(wěn)定性,感興趣的朋友跟隨小編一起看看吧2025-01-01使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03