您的位置:首页 > 编程语言 > Java开发

SpringMVC事件监听ApplicationListener实例解析

2019-12-11 12:11 2156 查看

这篇文章主要介绍了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 搜索。

以上就是本文的全部内容,希望对大家的学习有所帮助

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息