---
title: "Java Web项目中的Event"
author: "rensanning"
date: "2017-06-05T09:42:42Z"
tags: []
categories: ["企业架构"]
slug: "java-web-event"
draft: false
lastmod: "2017-06-05T10:14:52Z"
id: 2377751
url: "https://rensanning.iteye.com/blog/2377751"
---

\[b]（1）Servlet的事件和监听器\[/b] \[img]http://dl2.iteye.com/upload/attachment/0125/2759/3676c175-c965-3d13-bb81-b386e72503c3.png\[/img] \*\** Servlet、Filter、Listener 是单例的 \*\** 通过 @WebListener 或者 web.xml 来注册 \[b]（2）Spring 的 ApplicationEvent\[/b] \[quote]Publisher -&gt; (ApplicationEvent) -&gt; Listener\[/quote] \[b]a）Spring内置事件\[/b] \[quote]ContextRefreshedEvent ContextStartedEvent ContextStoppedEvent ContextClosedEvent RequestHandledEvent\[/quote] \[b]b）自定义事件\[/b] \[code="java"]public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } // 可以定义任意参数的构造函数，用于传递需要的参数 }\[/code] \[b]c）事件监听@EventListener、ApplicationListener\[/b] spring4.1之前： \[code="java"]public class MyListener1 implements ApplicationListener { @Override public void onApplicationEvent(ContextClosedEvent event) { } }\[/code] 从spring4.2开始可以 \[code="java"]public class MyListener2 { @EventListener public void processContextStoppedEvent(ContextStoppedEvent event) { } @EventListener public void processContextClosedEvent(ContextClosedEvent event) { } }\[/code] 也可以同时捕获多个事件 \[code="java"]@EventListener({ContextRefreshedEvent.class, ContextStoppedEvent.class, ContextStartedEvent.class}) public void handleContextEvent () { }\[/code] \[b]d）发布事件ApplicationEventPublisher\[/b] \[code="java"]@Autowired private ApplicationEventPublisher eventPublisher;\[/code] \[code="java"]eventPublisher.publishEvent(new MyEvent(this));\[/code] 也可以通过在Listener中再次返回一个Event来继续发布一个事件，从而实现发布多个事件。 \[code="java"]@EventListener public MyAnotherEvent handleMyEvent(MyEvent event) { // ... 处理MyEvent return new MyAnotherEvent(); }\[/code] \[b]e）异步执行\[/b] Listener默认是在发布事件的线程中执行的。 \[b]通过@Async实现异步\[/b] 把 @EventListener 标注的方法再标注 @Async 可实现异步执行。 \[b]通过ApplicationEventMulticaster实现异步\[/b] \[code="java"]@Bean ApplicationEventMulticaster applicationEventMulticaster() { SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); eventMulticaster.setErrorHandler(TaskUtils.LOG\_AND\_SUPPRESS\_ERROR\_HANDLER); return eventMulticaster; }\[/code] \[b]f）条件执行\[/b] 从spring4.3开始正式提供基于 SpEL 来指定事件执行的条件 \[code="java"]@EventListener(condition = "#myEvent.amount &gt;= 100") public void handleHighBids(MyEvent event) { // ... }\[/code] \[b]g）@TransactionalEventListener\[/b] 同步事件监听是的事务问题 比如需要在事务正确提交后在执行监听： \[code="java"]@TransactionalEventListener public void handleAfterCommit(TaskScheduledEvent event) // ... }\[/code] 或者指定在事务的某状态时执行监听： \[code="java"]@TransactionalEventListener(phase = TransactionPhase.AFTER\_ROLLBACK) public void handleAfterRollback(TaskScheduledEvent event) { // ... }\[/code] 参考： https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 http://zoltanaltfatter.com/2016/05/11/application-events-with-spring/ http://kimulla.hatenablog.com/entry/2016/09/23/125006