當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
004_SpringBoot整合Listener
生活随笔
收集整理的這篇文章主要介紹了
004_SpringBoot整合Listener
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 通過注解掃描完成Listener組件的注冊(SpringBoot整合Listener方式一)
1.1. 使用maven構建SpringBoot的名叫spring-boot-listener項目
1.2. pom.xml?
<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.bjbs</groupId><artifactId>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.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version></properties><dependencies><!-- springBoot的啟動器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>1.3. 編寫FirstListener.java
package com.bjbs1.controller;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener;/*** springBoot整合Listener方式一 ** <listener> * <listener-class>com.bjsxt1.listener.FirstListener</listener-class>* </listener>*/ @WebListener public class FirstListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent event) {}@Overridepublic void contextInitialized(ServletContextEvent event) {System.out.println("FirstListener...contextInitialized......");} }1.4. @WebListener注解
1.5. 編寫App.java啟動類?
package com.bjbs1;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;/*** springBoot整合Listener方式一, SpringBoot啟動類*/ @SpringBootApplication @ServletComponentScan // 在springBoot啟動時會掃描@WebListener, 并將該類實例化 public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }1.6. 配置@ServletComponentScan注解, 在SpringBoot啟動時會掃描@WebListener, 并將該類實例化
1.7. 運行項目?
2. 通過方法完成Listener組件注冊(SpringBoot整合Listener方式二)
2.1. 編寫SecondListener.java
package com.bjbs2.controller;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;/*** springBoot整合Listener方式二*/ public class SecondListener implements ServletContextListener {@Overridepublic void contextDestroyed(ServletContextEvent event) {}@Overridepublic void contextInitialized(ServletContextEvent event) {System.out.println("SecondListener...contextInitialized......");} }2.2. 編寫App2.java啟動類
package com.bjbs2;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.bjbs2.controller.SecondListener;/*** SpringBoot整合Listener方式二, SpringBoot啟動類*/ @SpringBootApplication public class App2 {public static void main(String[] args) {SpringApplication.run(App2.class, args);}/*** 注冊listener*/@Beanpublic ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean() {ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());return bean;} }2.3. 使用org.springframework.boot.web.servlet.ServletListenerRegistrationBean類注冊Listener
2.4. 運行項目?
總結
以上是生活随笔為你收集整理的004_SpringBoot整合Listener的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 003_SpringBoot整合Filt
- 下一篇: 007_SpringBoot文件上传