監(jiān)聽器(Listener)的注冊方法和 Servlet 一樣,有兩種方式:代碼注冊或者注解注冊
成都創(chuàng)新互聯(lián)公司公司2013年成立,先為舟山等服務(wù)建站,舟山等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為舟山企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
1.代碼注冊方式
通過代碼方式注入過濾器
@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"); } }
把注解加到入口處啟動即可
@SpringBootApplication @ServletComponentScan public class SpringBootSimpleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSimpleApplication.class, args); } }
以上所述是小編給大家介紹的Spring Boot的listener(監(jiān)聽器)簡單使用實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
當(dāng)前題目:SpringBoot的listener(監(jiān)聽器)簡單使用實例詳解
地址分享:http://jinyejixie.com/article32/iiccpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、企業(yè)建站、定制開發(fā)、商城網(wǎng)站、Google、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)