SpringMVC事件监听ApplicationListener实例解析

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringMVC事件监听ApplicationListener实例解析

  2021-04-02 我要评论

这篇文章主要介绍了SpringMVC事件监听ApplicationListener实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. 实现 ApplicationListener<T> 接口(T为监听类型,稍后会列出具体可监听事件)

2. 将该自定义监听类,注册为Spring容器组件。(即将该类注入Spring容器)

实例:

该类监听ServletRequestHandledEvent事件,该事件为请求结束回调事件,即一个请求完成结束后会执行onApplicationEvent内自定义业务逻辑。

package com.xxxxxx.xxxxxx.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.ServletRequestHandledEvent;

@Component
public class RequestedListener implements ApplicationListener<ServletRequestHandledEvent> {
  @Override
  public void onApplicationEvent(ServletRequestHandledEvent event) {
    System.out.println("requested event listener: " + event.getRequestUrl());
  }
}

可使用监听事件类型(所有可使用监听事件类型均继承自org.springframework.context.ApplicationEvent类):

- 具体监听事件可根据类型 google baidu 搜索。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们