Spring Boot的listener(監(jiān)聽(tīng)器)簡(jiǎn)單使用實(shí)例詳解
監(jiān)聽(tīng)器(Listener)的注冊(cè)方法和 Servlet 一樣,有兩種方式:代碼注冊(cè)或者注解注冊(cè)
1.代碼注冊(cè)方式
通過(guò)代碼方式注入過(guò)濾器
@Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new IndexListener()); return servletListenerRegistrationBean; }
IndexListener.Java類(lèi):
package com.example.Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class IndexListener implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("IndexListener contextDestroyed method"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("IndexListener contextInitialized method"); } }
2.注解方式
通過(guò)注解方式注入過(guò)濾器
IndexListener2.Java類(lèi)
package com.example.Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class IndexListener2 implements ServletContextListener{ @Override public void contextDestroyed(ServletContextEvent arg0) { System.out.println("IndexListener2 contextDestroyed method"); } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("IndexListener2 contextInitialized method"); } }
把注解加到入口處啟動(dòng)即可
@SpringBootApplication @ServletComponentScan public class SpringBootSimpleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSimpleApplication.class, args); } }
以上所述是小編給大家介紹的Spring Boot的listener(監(jiān)聽(tīng)器)簡(jiǎn)單使用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及Spr
這篇文章主要介紹了淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter
這篇文章主要介紹了dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java Jedis NOAUTH Authentication required問(wèn)題解決方法
這篇文章主要介紹了Java Jedis NOAUTH Authentication required問(wèn)題解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07解決mybatis批量更新出現(xiàn)SQL報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了mybatis批量更新出現(xiàn)SQL報(bào)錯(cuò),解決辦法也很簡(jiǎn)單只需要在application.properties配置文中的數(shù)據(jù)源url后面添加一個(gè)參數(shù),需要的朋友可以參考下2022-02-02SpringSessionRedis配置及發(fā)現(xiàn)的問(wèn)題講解
今天小編就為大家分享一篇關(guān)于SpringSessionRedis配置及發(fā)現(xiàn)的問(wèn)題講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03SpringBoot、mybatis返回樹(shù)結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn)
本文主要介紹了SpringBoot、mybatis返回樹(shù)結(jié)構(gòu)的數(shù)據(jù)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)