日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springBoot整合Listener

發布時間:2024/4/13 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springBoot整合Listener 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot當中是如何去整合Listener,其實SpringBoot去整合Listener,和整合Servlet,Filter比較相似,也是提供了兩種整合方式,第一種仍然是通過注解,掃描的方式去完成注冊,通過注解掃描完成Listener的注冊,然后第二種方式,通過方法完成Listener組件的注冊,我們先來看第一種方式,首先我們創建一個項目,我們首先要創建一個Listener,這個時候我們要實現一個接口,取決于我們要做那個監聽器,比如我們要做一個Servlet上下文的監聽器,實現ServletContextListener的接口,然后需要一個@WebListener的注解,這里我們不需要配置什么,我們以往在web.xml怎么配置Listener,他相比于Servlet和Filter要容易的多,我們在web.xml當中,在listener當中有一個listener-class,在listener-class當中呢,所以這里沒有urlpatterns的配置,所以我們這里只需要WebListener就可以了,這樣我們的Listener就創建好了@WebListener public class FirstListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("Listener...init......");}}這樣我們一個監聽器就寫好了,第二步編寫啟動類@SpringBootApplication @ServletComponentScan public class ListenerApp {public static void main(String[] args) {SpringApplication.run(ListenerApp.class, args);}}這個啟動器和其他的啟動器是一樣的,@SpringBootApplication加一個@ServletComponentScan,這樣這個啟動器就創建好了,啟動的時候觀察一下控制臺,控制臺輸出了表示我們的監聽器已經為我們工作了,那么以上的方式就是基于掃描的方式,我們來看第二種方式,通過方法完成組件的注冊,第一步我們還是要編寫一個Listener,他也是去實現一個ServletContextListener的接口public class SecondListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("SecondListener..init.....");}}在這個監聽器當中呢,也是不需要加任何的Annotation了,因為我們采用的是方法注冊方式,這樣我們的監聽器就寫好了,第二步編寫啟動類,我們再次創建啟動類@SpringBootApplication public class ListenerApp2 {public static void main(String[] args) {SpringApplication.run(ListenerApp2.class, args);}/*** 注冊listener*/@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;} }但是我們這里要提供一個方法,這個方法的作用就是,用于去注冊我們的Listener,這個方法叫什么名無所謂,但是他必須要有一個返回值,返回值的類型是什么呢,叫ServletListenerRegistrationBean,然后這里需要一個泛型,就是你當前要注冊的Listener的類型,然后在這里去實例化這個對象,在這里去new一個監聽器對象,然后將這個bean對象返回,別忘了在這個方法上加一個Bean的Annotation,那么就完成了監聽器的第二種方式,我們來運行一下看是否生效,我們看到SecondListener已經工作了,那么以上就是Listener第二種方式的一種實現 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>04-spring-boot-Listener</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> </dependencies> </project> package com.learn.listener;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;/*** springBoot整合Listener**<listener>* <listener-class>com.learn.listener.FirstListener</listener-class>*</listener>*/ @WebListener public class FirstListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("Listener...init......");}} package com.learn;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;/*** springBoot整合Listener方式一*/ @SpringBootApplication @ServletComponentScan public class ListenerApp {public static void main(String[] args) {SpringApplication.run(ListenerApp.class, args);}} package com.learn.listener;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;/*** springBoot整合Listener方式二。*/ public class SecondListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent arg0) {}@Overridepublic void contextInitialized(ServletContextEvent arg0) {System.out.println("SecondListener..init.....");}} package com.learn;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 com.learn.listener.SecondListener;/*** SpringBoot整合Listener方式二*/ @SpringBootApplication public class ListenerApp2 {public static void main(String[] args) {SpringApplication.run(ListenerApp2.class, args);}/*** 注冊listener*/@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;} }

?

總結

以上是生活随笔為你收集整理的springBoot整合Listener的全部內容,希望文章能夠幫你解決所遇到的問題。

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