spring boot slf4j logback listener filter json aop http get scheduling tasks

Spring Boot EP 14:使用Listener監聽器管理Context、Request與Session的生命週期

Listener監聽器的分類

以用途來分,大致有3種監聽器(servlet轄下,詳細如官方網站),分別監聽對象為Context、Request、Session,並各自有不同的實現,如下表:

監聽對象初始化/銷毀屬性異動
ContextServletContextListenerServletContextAttributeListener
RequestServletRequestListenerServletRequestAttributeListener
SessionHttpSessionListenerHttpSessionAttributeListener

每個監聽器其對應的方法及觸發事件如下:

監聽器方法及觸發事件
ServletContextListener– contextInitialized(ServletContextEvent arg0) => 初始化
– contextDestroyed(ServletContextEvent arg0) => 銷毀
ServletContextAttributeListener– attributeAdded(ServletContextAttributeEvent event) => 新增屬性
– attributeReplaced(ServletContextAttributeEvent event) => 修改屬性
– attributeRemoved(ServletContextAttributeEvent event) => 刪除屬性
ServletRequestListener– requestInitialized(ServletRequestEvent sre) => 初始化
– requestDestroyed(ServletRequestEvent sre) => 銷毀
ServletRequestAttributeListener– attributeAdded(ServletRequestAttributeEvent srae) => 新增屬性
– attributeReplaced(ServletRequestAttributeEvent srae) => 修改屬性
– attributeRemoved(ServletRequestAttributeEvent srae) => 刪除屬性
HttpSessionListener– sessionCreated(HttpSessionEvent se) => 初始化
– sessionDestroyed(HttpSessionEvent se) => 銷毀
HttpSessionAttributeListener– attributeAdded(HttpSessionBindingEvent event) => 新增屬性
– attributeReplaced(HttpSessionBindingEvent event) => 修改屬性
– attributeRemoved(HttpSessionBindingEvent event) => 刪除屬性

實現以上所有的Listener

這裡提供實現這些Listener的範例,因為實在想不出好的實際應用,就分享空的類別原始碼供需要的人參考。

Context監聽器:ServletContextListener, ServletContextAttributeListener

package stockmarket.jovepater.com.stockmarket.Listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.stereotype.Component;

@Component
public class ContextListener implements ServletContextListener, ServletContextAttributeListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        ServletContextListener.super.contextInitialized(sce);
        System.out.println("ServletContextListener: Context Initialized");
    }

    ServletContext servletContext;

    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        // TODO Auto-generated method stub
        ServletContextAttributeListener.super.attributeAdded(scae);
        System.out.println("ServletContextAttributeListener: Context Attribute Added");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        // TODO Auto-generated method stub
        ServletContextAttributeListener.super.attributeRemoved(scae);
        System.out.println("ServletContextAttributeListener: Context Attribute Removed");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        // TODO Auto-generated method stub
        ServletContextAttributeListener.super.attributeReplaced(scae);
        System.out.println("ServletContextAttributeListener: Context Attribute Replaced");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        ServletContextListener.super.contextDestroyed(sce);
        System.out.println("ServletContextListener: Context Destroyed");
    }

}

Request監聽器:ServletRequestListener, ServletRequestAttributeListener

package stockmarket.jovepater.com.stockmarket.Listener;

import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

import org.springframework.stereotype.Component;

@Component
public class RequestListener implements ServletRequestListener, ServletRequestAttributeListener {

    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        ServletRequestListener.super.requestInitialized(sre);
        System.out.println("ServletRequestListener: Request Initialized");
    }

    @Override
    public void attributeAdded(ServletRequestAttributeEvent srae) {
        // TODO Auto-generated method stub
        ServletRequestAttributeListener.super.attributeAdded(srae);
        System.out.println("ServletRequestAttributeListener: Request Attribute Added");
    }

    @Override
    public void attributeRemoved(ServletRequestAttributeEvent srae) {
        // TODO Auto-generated method stub
        ServletRequestAttributeListener.super.attributeRemoved(srae);
        System.out.println("ServletRequestAttributeListener: Request Attribute Removed");
    }

    @Override
    public void attributeReplaced(ServletRequestAttributeEvent srae) {
        // TODO Auto-generated method stub
        ServletRequestAttributeListener.super.attributeReplaced(srae);
        System.out.println("ServletRequestAttributeListener: Request Attribute Replaced");
    }

    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        // TODO Auto-generated method stub
        ServletRequestListener.super.requestDestroyed(sre);
        System.out.println("ServletRequestListener: Request Destroyed");
    }

}

Session監聽器:HttpSessionListener, HttpSessionAttributeListener

package stockmarket.jovepater.com.stockmarket.Listener;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.stereotype.Component;

@Component
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        // TODO Auto-generated method stub
        HttpSessionListener.super.sessionCreated(se);
        System.out.println("HttpSessionListener: Session Created");
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        // TODO Auto-generated method stub
        HttpSessionAttributeListener.super.attributeAdded(se);
        System.out.println("HttpSessionAttributeListener: Session Attribute Added");
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        // TODO Auto-generated method stub
        HttpSessionAttributeListener.super.attributeRemoved(se);
        System.out.println("HttpSessionAttributeListener: Session Attribute Removed");
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent se) {
        // TODO Auto-generated method stub
        HttpSessionAttributeListener.super.attributeReplaced(se);
        System.out.println("HttpSessionAttributeListener: Session Attribute Replaced");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        // TODO Auto-generated method stub
        HttpSessionListener.super.sessionDestroyed(se);
        System.out.println("HttpSessionListener: Session Destroyed");
    }

}

~ END ~


, , , ,

Related posts

Latest posts