欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot的listener(監(jiān)聽器)簡單使用實(shí)例詳解

 更新時(shí)間:2017年04月24日 14:21:09   作者:牛頭人  
監(jiān)聽器(Listener)的注冊方法和 Servlet 一樣,有兩種方式:代碼注冊或者注解注冊。接下來通過本文給大家介紹Spring Boot的listener(監(jiān)聽器)簡單使用,需要的朋友可以參考下

監(jiān)聽器(Listener)的注冊方法和 Servlet 一樣,有兩種方式:代碼注冊或者注解注冊

1.代碼注冊方式

通過代碼方式注入過濾器

 @Bean
  public ServletListenerRegistrationBean servletListenerRegistrationBean(){
    ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
    servletListenerRegistrationBean.setListener(new IndexListener());
    return servletListenerRegistrationBean;
  }

IndexListener.Java類:

package com.example.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class IndexListener implements ServletContextListener{
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("IndexListener contextDestroyed method");
  }
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("IndexListener contextInitialized method");
  }
}

2.注解方式

通過注解方式注入過濾器

IndexListener2.Java類

package com.example.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class IndexListener2 implements ServletContextListener{
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("IndexListener2 contextDestroyed method");
  }
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    System.out.println("IndexListener2 contextInitialized method");
  }
}

把注解加到入口處啟動(dòng)即可

@SpringBootApplication
@ServletComponentScan
public class SpringBootSimpleApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootSimpleApplication.class, args);
  }
}

以上所述是小編給大家介紹的Spring Boot的listener(監(jiān)聽器)簡單使用實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)

    java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)

    本篇文章主要介紹了java實(shí)現(xiàn)excel和txt文件互轉(zhuǎn)的相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • ThreadLocal原理及內(nèi)存泄漏原因

    ThreadLocal原理及內(nèi)存泄漏原因

    這篇文章主要介紹了ThreadLocal原理及內(nèi)存泄漏原因,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 最新評論