详解springboot整合侦听器的两种方式

  

<强> 1。通过注解

  

<强> 详解springboot整合侦听器的两种方式

  

编写启动类

        包cn.bl;   进口org.springframework.boot.SpringApplication;   进口org.springframework.boot.autoconfigure.SpringBootApplication;   进口org.springframework.boot.web.servlet.ServletComponentScan;   @SpringBootApplication   @ServletComponentScan   公共类应用{   公共静态void main (String [] args) {   SpringApplication.run (App.class, args);   }   }      

,编写一个监听器

        包cn.bl.listener;   进口javax.servlet.ServletContextEvent;   进口javax.servlet.ServletContextListener;   进口javax.servlet.annotation.WebListener;   @WebListener   公共类FirstListener实现ServletContextListener {   @Override   公共空间contextInitialized (ServletContextEvent sce) {   system . out。println (“init . .");   }   @Override   公共空间contextDestroyed (ServletContextEvent sce) {   system . out。println(“被毁。");   }   }      

,当执行程序的时候

  

详解springboot整合侦听器的两种方式

  

<强> 2。通过函数

  

<强> 详解springboot整合侦听器的两种方式

        包cn.bl.listener;   进口javax.servlet.ServletContextEvent;   进口javax.servlet.ServletContextListener;   公共类SecondListener实现ServletContextListener {   @Override   公共空间contextInitialized (ServletContextEvent sce) {   system . out。println(“第二servletListener init . .");   }   @Override   公共空间contextDestroyed (ServletContextEvent sce) {   system . out。println(“第二servletListener毁灭. .");   }   }      包cn.bl;         进口org.springframework.boot.SpringApplication;   进口org.springframework.boot.autoconfigure.SpringBootApplication;   进口org.springframework.boot.web.servlet.ServletListenerRegistrationBean;   进口org.springframework.context.annotation.Bean;   进口cn.bl.listener.SecondListener;   @SpringBootApplication   公开课App2 {   公共静态void main (String [] args) {   SpringApplication.run (App2.class, args);   }   @ bean   公共ServletListenerRegistrationBean getBean () {   ServletListenerRegistrationBean之前      

  

以上所述是小编给大家介绍的springboot整合侦听器的两种方式,希望对大家有所帮助,如果大家有任何疑问请给我留的言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

详解springboot整合侦听器的两种方式