詳解springboot整合Listener的兩種方式

1.通過注解

成都創(chuàng)新互聯(lián)公司專注于格爾木企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城建設(shè)。格爾木網(wǎng)站建設(shè)公司,為格爾木等地區(qū)提供建站服務(wù)。全流程按需設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

詳解springboot整合Listener的兩種方式

編寫啟動(dòng)類

package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
 public static void main(String[] args) {
 SpringApplication.run(App.class, args);
 }
}

 編寫一個(gè)監(jiān)聽器

package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class FirstListener implements ServletContextListener{
 @Override
 public void contextInitialized(ServletContextEvent sce) {
 System.out.println("init .. ");
 }
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
 System.out.println("desroyed .. ");
 }
}

 當(dāng)執(zhí)行App的時(shí)候

詳解springboot整合Listener的兩種方式

2.通過函數(shù)

詳解springboot整合Listener的兩種方式

package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener{
 @Override
 public void contextInitialized(ServletContextEvent sce) {
 System.out.println("second servletListener init .. ");
 }
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
 System.out.println("second servletListener destroy .. ");
 }
}

package cn.bl;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import cn.bl.listener.SecondListener;
@SpringBootApplication
public class App2 {
 public static void main(String[] args) {
 SpringApplication.run(App2.class, args);
 }
 @Bean
 public ServletListenerRegistrationBean<SecondListener>getBean(){
 ServletListenerRegistrationBean<SecondListener>bean = new ServletListenerRegistrationBean<>(new SecondListener());
 return bean;
 }
}

總結(jié)

以上所述是小編給大家介紹的springboot整合Listener的兩種方式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

當(dāng)前名稱:詳解springboot整合Listener的兩種方式
文章來源:http://bm7419.com/article14/jddpde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司ChatGPT、虛擬主機(jī)、企業(yè)建站、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)