简介
- Spring Listener是用于指定对象发生事件或状态改变的监听器
常见4种监听方式
- 监听 Servlet上下文对象
public class Test implements Serializable {
private static final long serialVersionUID = -5442854457404278567L;
private String name;
public Test(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Service
public class TestService {
public Test getTestObject() {
return new Test("全爷 "+ System.currentTimeMillis());
}
}
/**
* 监听器, 实现 ApplicationListener接口
* */
@Component
public class TestServletContextListener implements ApplicationListener<ContextRefreshedEvent> {
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
/** 获取上下文*/
applicationContext = contextRefreshedEvent.getApplicationContext();
/** 将数据载入到应用域中*/
onRefresh();
}
public void onRefresh() {
/** 获取指定服务*/
final TestService testService = applicationContext.getBean(TestService.class);
/** 通过指定方法获取业务数据*/
final Test test = testService.getTestObject();
/** 获取应用域对象*/
final ServletContext application = applicationContext.getBean(ServletContext.class);
/** 将数据存到应用域中*/
application.setAttribute("testObject", test);
}
}
@RestController
public class TestController {
@GetMapping("/index")
public Map<String, Object> index(HttpServletRequest request) {
ServletContext application = request.getServletContext();
/** 从应用域中获取对象*/
final Test test = (Test) application.getAttribute("testObject");
final Map<String, Object> result = new HashMap<>();
result.put("name", test.getName());
return result;
}
@Autowired
TestServletContextListener testServletContextListener;
@GetMapping("/refresh")
public Map<String, Object> refresh(HttpServletRequest request) {
ServletContext application = request.getServletContext();
/** 在应用域中删除指定对象*/
application.removeAttribute("testObject");
/** 将数据载入到应用域中*/
testServletContextListener.onRefresh();
final Map<String, Object> result = new HashMap<>();
result.put("refresh", true);
return result;
}
}
# 请求地址 http://127.0.0.1:8080/index
# 输出
{"name":"全爷 1587382849292"}
# 请求地址 http://127.0.0.1:8080/refresh
# 输出
{"refresh":true}
- 监听 HTTP请求 Servlet Request对象
- 监听, 用户请求信息
@Component
public class TestServletRequestListener implements ServletRequestListener {
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
System.out.println("Request init sessionId: " + request.getRequestedSessionId() + ", URL: " + request.getRequestURL());
request.setAttribute("name", "全爷");
}
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
HttpServletRequest request = (HttpServletRequest) servletRequestEvent.getServletRequest();
System.out.println("Request end name: " + request.getAttribute("name"));
}
}
@RestController
public class TestController {
@GetMapping("/index")
public Map<String, Object> index(HttpServletRequest request) {
final Map<String, Object> result = new HashMap<>();
result.put("name", request.getAttribute("name"));
return result;
}
}
# 请求地址 http://127.0.0.1:8080/index
# 输出
{"name":"全爷"}
# Console
Request init sessionId: null, URL: http://127.0.0.1:8080/index
Request end name: 全爷
Request init sessionId: null, URL: http://127.0.0.1:8080/favicon.ico
Request end name: 全爷
- 监听 HTTP会话 Session对象
- 监听, 在线用户数
/**
* 记录在线的用户数
*/
@Component
public class TestHttpSessionListener implements HttpSessionListener {
@Override
public synchronized void sessionCreated(HttpSessionEvent httpSessionEvent) {
Long count = (Long) httpSessionEvent.getSession().getServletContext().getAttribute("count");
if (count == null) {
count = 1L;
} else {
++count;
}
System.out.println("新用户上线了: " + count);
httpSessionEvent.getSession().getServletContext().setAttribute("count", count);
}
@Override
public synchronized void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
Long count = (Long) httpSessionEvent.getSession().getServletContext().getAttribute("count");
System.out.println("用户下线了: " + --count);
httpSessionEvent.getSession().getServletContext().setAttribute("count", count);
}
}
@RestController
public class TestController {
@GetMapping("/index")
public Map<String, Object> index(HttpServletRequest request) {
final Map<String, Object> result = new HashMap<>();
result.put("count", request.getSession().getServletContext().getAttribute("count"));
return result;
}
}
# 请求地址 http://127.0.0.1:8080/index
# 输出
{"count":1}
注: 测试时环境中设置 Session过期时间设置小一些
server.servlet.session.timeout=10s
- 监听自定义对象事件监听
/**
* 自定义事件
*/
public class CustomEvent extends ApplicationEvent {
private static final long serialVersionUID = -3106941444235503333L;
private Test test;
public CustomEvent(Object source, Test test) {
super(source);
this.test = test;
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
}
/**
* 自定义监听器
* 实现 ApplicationListener接口, 监听 CustomEvent事件
* */
@Component
public class CustomEventListener implements ApplicationListener<CustomEvent> {
@Override
public void onApplicationEvent(CustomEvent customEvent) {
/** 从自定义事件获取信息*/
Test test = customEvent.getTest();
/** 在此处, 处理相关业务*/
System.out.println("name: " + test.getName());
}
}
@Service
public class TestService {
@Resource
private ApplicationContext applicationContext;
public Test triggerEvent() {
Test test = new Test("全爷 "+ System.currentTimeMillis());
/** 触发事件*/
CustomEvent event = new CustomEvent(this, test);
applicationContext.publishEvent(event);
return test;
}
}
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/index")
public Map<String, Object> index() {
testService.triggerEvent();
final Map<String, Object> result = new HashMap<>();
result.put("event", true);
return result;
}
}
# 请求地址 http://127.0.0.1:8080/index
# 输出
{"event":true}
# Console
name: 全爷 1587447532296
如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!