日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

第四篇:Spring Boot 整合listener

發布時間:2024/9/27 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第四篇:Spring Boot 整合listener 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Spring Boot整合listener

第一種方案:通過注解掃描完成Listener的注冊

  • 1.1 編寫一個listener
@WebListener public class FirstListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("FirstListener init .....");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {} }
  • 1.2編寫一個啟動類,并在啟動類上添加注解
/*** SpringBoot整合Listener 方式 1*/ @SpringBootApplication @ServletComponentScan public class SpringBootChapter1Application {public static void main(String[] args) {SpringApplication.run(SpringBootChapter1Application.class, args);} }
  • 1.3 啟動項目及查看控制臺

第二種方案:通過方法完成Listener的注冊

  • 2.1編寫一個listener
  • 以前實現方式
<listener><listener-class>com.gblfy.listener.FirstListener</listener-class> </listener>
  • Spring Boot實現方式:
//SpringBoot整合Listener 方式 2 public class SecondListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("Secondlistener init .....");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {} }
  • 2.2 編一個啟動類
/*** SpringBoot整合Listener 方式 2*/ @SpringBootApplication public class SpringbootListenerApplication2 {public static void main(String[] args) {SpringApplication.run(SpringbootListenerApplication2.class, args);}@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;} }
  • 2.3 啟動項目及查看控制臺

本文源碼下載:

github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-4

總結

以上是生活随笔為你收集整理的第四篇:Spring Boot 整合listener的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。