首页 > 编程 > Java > 正文

SpringMVC事件监听ApplicationListener实例解析

2019-11-26 06:24:49
字体:
来源:转载
供稿:网友

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表