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

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

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

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

1.代碼注冊(cè)方式

通過(guò)代碼方式注入過(guò)濾器

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

IndexListener.Java類(lèi):

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.注解方式

通過(guò)注解方式注入過(guò)濾器

IndexListener2.Java類(lèi)

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)聽(tīng)器)簡(jiǎn)單使用實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(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)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • ThreadLocal原理及內(nèi)存泄漏原因

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

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