springboot listener注册(ServletListenerRegistrationBean实现)

springboot listener注册

 

应用:对相关事件进行监听,做出相应的处理

 

 

********************

相关类及接口

 

ServletListenerRegistrationBean:注册listener

public class ServletListenerRegistrationBean<T extends EventListener> extends RegistrationBean {
    private static final Set<Class<?>> SUPPORTED_TYPES;
    private T listener;

********
构造方法

    public ServletListenerRegistrationBean() {
    public ServletListenerRegistrationBean(T listener) {

********
常用方法

    public void setListener(T listener) {
    public T getListener() {

 

request相关监听接口

ServletRequestListener:request监听接口

public interface ServletRequestListener extends EventListener {
    default void requestDestroyed(ServletRequestEvent sre) {
    }

    default void requestInitialized(ServletRequestEvent sre) {
    }
}

****************************
ServletRequestAttributeListener:request属性监听接口

public interface ServletRequestAttributeListener extends EventListener {
    default void attributeAdded(ServletRequestAttributeEvent srae) {
    }

    default void attributeRemoved(ServletRequestAttributeEvent srae) {
    }

    default void attributeReplaced(ServletRequestAttributeEvent srae) {
    }
}

 

Session相关监听接口

HttpSessionListener:session监听接口

public interface HttpSessionListener extends EventListener {
    default void sessionCreated(HttpSessionEvent se) {
    }

    default void sessionDestroyed(HttpSessionEvent se) {
    }
}

*****************************
HttpSessionIdListener:sessionId监听接口

public interface HttpSessionIdListener extends EventListener {
    void sessionIdChanged(HttpSessionEvent var1, String var2);
}

*******************************
HttpSessionAttributeListener:session属性监听接口

public interface HttpSessionAttributeListener extends EventListener {
    default void attributeAdded(HttpSessionBindingEvent se) {
    }

    default void attributeRemoved(HttpSessionBindingEvent se) {
    }

    default void attributeReplaced(HttpSessionBindingEvent se) {
    }
}

***************************
HttpSessionActivationListener:session激活监听接口

public interface HttpSessionActivationListener extends EventListener {
    default void sessionWillPassivate(HttpSessionEvent se) {
    }

    default void sessionDidActivate(HttpSessionEvent se) {
    }
}

****************************
HttpSessionBindingListener:session绑定监听接口

public interface HttpSessionBindingListener extends EventListener {
    default void valueBound(HttpSessionBindingEvent event) {
    }

    default void valueUnbound(HttpSessionBindingEvent event) {
    }
}

 

Servlet 监听接口

ServletContextListener:容器监听接口

public interface ServletContextListener extends EventListener {
    default void contextInitialized(ServletContextEvent sce) { //容器启动执行操作
    }

    default void contextDestroyed(ServletContextEvent sce) {   //容器关闭执行操作
    }
}


****************************
ServletContextAttributeListener:容器属性监听接口

public interface ServletContextAttributeListener extends EventListener {
    default void attributeAdded(ServletContextAttributeEvent scae) {
    }

    default void attributeRemoved(ServletContextAttributeEvent scae) {
    }

    default void attributeReplaced(ServletContextAttributeEvent scae) {
    }
}

 

其他监听接口

AsyncListener:异步监听接口

public interface AsyncListener extends EventListener {

    void onComplete(AsyncEvent var1) throws IOException;
    void onTimeout(AsyncEvent var1) throws IOException;
    void onError(AsyncEvent var1) throws IOException;
    void onStartAsync(AsyncEvent var1) throws IOException;
}

**************************
ReadListener:读数据接口

public interface ReadListener extends EventListener {

    void onDataAvailable() throws IOException;
    void onAllDataRead() throws IOException;
    void onError(Throwable var1);
}

 

 

********************

示例

 

******************

自定义listener

 

MyListener:容器启动关闭时执行自定义操作

public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("容器初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("容器销毁");
    }
}

 

******************

config 层

 

WebConfig:注册listener

@Configuration
public class WebConfig implements WebMvcConfigurer{

    @Bean
    public ServletListenerRegistrationBean<MyListener> initServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean=new ServletListenerRegistrationBean<>();
        servletListenerRegistrationBean.setListener(new MyListener());

        return servletListenerRegistrationBean;
    }
}

 

 

********************

应用启动控制台输出:

 

2019-12-18 10:14:50.968  INFO 18348 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 889 ms
2019-12-18 10:14:51.025  INFO 18348 --- [           main] o.apache.catalina.core.StandardContext   : Suspicious URL pattern: [/**] in context [], see sections 12.1 and 12.2 of the Servlet specification
容器初始化
2019-12-18 10:14:51.139  INFO 18348 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-18 10:14:51.267  INFO 18348 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-12-18 10:14:51.272  INFO 18348 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.662 seconds (JVM running for 3.192)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值