Spring Boot的listener(監(jiān)聽器)簡(jiǎn)單使用實(shí)例詳解
監(jiān)聽器(Listener)的注冊(cè)方法和 Servlet 一樣,有兩種方式:代碼注冊(cè)或者注解注冊(cè)
1.代碼注冊(cè)方式
通過代碼方式注入過濾器
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new IndexListener());
return servletListenerRegistrationBean;
}
IndexListener.Java類:
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.注解方式
通過注解方式注入過濾器
IndexListener2.Java類
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)聽器)簡(jiǎn)單使用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
SpringBoot定制化Starter實(shí)現(xiàn)方法
小伙伴們?cè)?jīng)可能都經(jīng)歷過整天寫著CURD的業(yè)務(wù),都沒寫過一些組件相關(guān)的東西,這篇文章記錄一下SpringBoot如何自定義一個(gè)Starter。原理和理論就不用多說了,可以在網(wǎng)上找到很多關(guān)于該方面的資料,這里主要分享如何自定義2023-01-01
Spring Boot實(shí)現(xiàn)簡(jiǎn)單的增刪改查
這篇文章主要介紹了Spring Boot如何實(shí)現(xiàn)簡(jiǎn)單的增刪改查,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下2020-09-09
Springboot+WebSocket+Netty實(shí)現(xiàn)在線聊天/群聊系統(tǒng)
這篇文章主要實(shí)現(xiàn)在好友添加、建群、聊天對(duì)話、群聊功能,使用Java作為后端語(yǔ)言進(jìn)行支持,界面友好,開發(fā)簡(jiǎn)單,文章中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-08-08
如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證
這篇文章主要介紹了如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
詳解java如何處理各種批量數(shù)據(jù)入庫(kù)
這篇文章主要為大家詳細(xì)介紹了java如何使用BlockingQueue處理各種批量數(shù)據(jù)入庫(kù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
java開發(fā)中為什么雙重效驗(yàn)鎖要加volatile
這篇文章主要為大家介紹了java開發(fā)中為什么雙重效驗(yàn)鎖要加volatile原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
springboot mybatis-plus實(shí)現(xiàn)登錄接口
本文主要介紹了springboot mybatis-plus實(shí)現(xiàn)登錄接口,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
SpringBoot全局異常處理之多個(gè)處理器匹配順序(最新推薦)
這篇文章主要介紹了SpringBoot全局異常處理之多個(gè)處理器匹配順序(最新推薦),調(diào)試源碼可見匹配順序?yàn)椋寒惓蛹?jí)高者優(yōu)先,再清楚點(diǎn),子類異常處理器優(yōu)先,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-03-03

