java web三要素Filter, Servlet前面分別進行了介紹,接下來咱們看一下Listener的相關知識點,本篇博文主要內容爲SpringBoot環境下,如何自定義Listener並註冊到spring容器java
<!-- more -->git
首先咱們須要搭建一個web工程,以方便後續的servelt註冊的實例演示,能夠經過spring boot官網建立工程,也能夠創建一個maven工程,在pom.xml中以下配置github
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </pluginManagement> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot-local</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release-local</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>
咱們這裏說到的Listener專指java web相關的監聽器,與Spring自己的Listener並不同。在java web中Listener的知識點爲servlet規範的那一套,這裏不詳細展開。下面主要介紹在SpringBoot中使用Servlet Listener的四種方式web
@WebListener
註解爲Servlet3+提供的註解,能夠標識一個類爲Listener,使用姿式和前面的Listener、Filter並無太大的區別spring
@WebListener public class AnoContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("@WebListener context 初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("@WebListener context 銷燬"); } }
由於WebListener註解不是spring的規範,因此爲了識別它,須要在啓動類上添加註解@ServletComponentScan
websocket
@ServletComponentScan @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
第二種使用方式是將Listener當成一個普通的spring bean,spring boot會自動將其包裝爲ServletListenerRegistrationBean
對象socket
@Component public class BeanContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("bean context 初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("bean context 銷燬"); } }
經過java config來主動將一個普通的Listener對象,塞入ServletListenerRegistrationBean
對象,建立爲spring的bean對象maven
public class ConfigContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("config context 初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("java context 銷燬"); } }
上面只是一個普通的類定義,下面的bean建立纔是關鍵點ide
@Bean public ServletListenerRegistrationBean configContextListener() { ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); bean.setListener(new ConfigContextListener()); return bean; }
這裏主要是藉助在ServletContext上下文建立的實際,主動的向其中添加Filter,Servlet, Listener,從而實現一種主動註冊的效果spring-boot
public class SelfContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContextInitializer context 初始化"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContextInitializer context 銷燬"); } } @Component public class ExtendServletConfigInitializer implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addListener(SelfContextListener.class); } }
注意ExtendServletConfigInitializer的主動註冊時機,在啓動時添加了這個Listenrer,因此它的優先級會是最高
上面介紹了四種註冊方式,均可以生效,在咱們的實際開發中,按需選擇一種便可,不太建議多種方式混合使用;
項目啓動和關閉以後,輸出日誌以下
盡信書則不如,以上內容,純屬一家之言,因我的能力有限,不免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
下面一灰灰的我的博客,記錄全部學習和工做中的博文,歡迎你們前去逛逛