22
33
44
5- # Spring 中的事件处理
5+ # Spring 中的事件处理
66
77
88
9- 2022-05-16 15:29 更新
9+ 2022-05-16 15:29 更新
1010
1111
1212
1616
1717
1818
19- ## Spring 中的事件处理
19+ ## Spring 中的事件处理
2020
21- 你已经看到了在所有章节中 Spring 的核心是 ** ApplicationContext** ,它负责管理 beans 的完整生命周期。当加载 beans 时,ApplicationContext 发布某些类型的事件。例如,当上下文启动时,ContextStartedEvent 发布,当上下文停止时,ContextStoppedEvent 发布。
21+ 你已经看到了在所有章节中 Spring 的核心是 ** ApplicationContext** ,它负责管理 beans 的完整生命周期。当加载 beans 时,ApplicationContext 发布某些类型的事件。例如,当上下文启动时,ContextStartedEvent 发布,当上下文停止时,ContextStoppedEvent 发布。
2222
23- 通过 ApplicationEvent 类和 ApplicationListener 接口来提供在 ApplicationContext 中处理事件。如果一个 bean 实现 ApplicationListener,那么每次 ApplicationEvent 被发布到 ApplicationContext 上,那个 bean 会被通知。
23+ 通过 ApplicationEvent 类和 ApplicationListener 接口来提供在 ApplicationContext 中处理事件。如果一个 bean 实现 ApplicationListener,那么每次 ApplicationEvent 被发布到 ApplicationContext 上,那个 bean 会被通知。
2424
25- Spring 提供了以下的标准事件:
25+ Spring 提供了以下的标准事件:
2626
27- | 序号 | Spring 内置事件 & 描述 |
27+ | 序号 | Spring 内置事件 & 描述 |
2828| --- | --- |
29- | 1 | ** ContextRefreshedEvent** ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法来发生。 |
30- | 2 | ** ContextStartedEvent** 当使用 ConfigurableApplicationContext 接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。 |
31- | 3 | ** ContextStoppedEvent** 当使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作。 |
32- | 4 | ** ContextClosedEvent** 当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。 |
33- | 5 | ** RequestHandledEvent** 这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。 |
29+ | 1 | ** ContextRefreshedEvent** ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法来发生。 |
30+ | 2 | ** ContextStartedEvent** 当使用 ConfigurableApplicationContext 接口中的 start() 方法启动 ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序。 |
31+ | 3 | ** ContextStoppedEvent** 当使用 ConfigurableApplicationContext 接口中的 stop() 方法停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作。 |
32+ | 4 | ** ContextClosedEvent** 当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启。 |
33+ | 5 | ** RequestHandledEvent** 这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。 |
3434
35- 由于 Spring 的事件处理是单线程的,所以如果一个事件被发布,直至并且除非所有的接收者得到的该消息,该进程被阻塞并且流程将不会继续。因此,如果事件处理被使用,在设计应用程序时应注意。
35+ 由于 Spring 的事件处理是单线程的,所以如果一个事件被发布,直至并且除非所有的接收者得到的该消息,该进程被阻塞并且流程将不会继续。因此,如果事件处理被使用,在设计应用程序时应注意。
3636
37- ## 监听上下文事件
37+ ## 监听上下文事件
3838
39- 为了监听上下文事件,一个 bean 应该实现只有一个方法 ** onApplicationEvent()** 的 ApplicationListener 接口。因此,我们写一个例子来看看事件是如何传播的,以及如何可以用代码来执行基于某些事件所需的任务。
39+ 为了监听上下文事件,一个 bean 应该实现只有一个方法 ** onApplicationEvent()** 的 ApplicationListener 接口。因此,我们写一个例子来看看事件是如何传播的,以及如何可以用代码来执行基于某些事件所需的任务。
4040
41- 让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:
41+ 让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:
4242
43- | 步骤 | 描述 |
43+ | 步骤 | 描述 |
4444| --- | --- |
45- | 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 ** src** 文件夹中创建一个包 com.tutorialspoint。 |
46- | 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 |
47- | 3 | 在 com.tutorialspoint 包中创建 Java 类 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp。 |
48- | 4 | 在 ** src** 文件夹中创建 Bean 的配置文件 Beans.xml。 |
49- | 5 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。 |
45+ | 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 ** src** 文件夹中创建一个包 com.tutorialspoint。 |
46+ | 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 |
47+ | 3 | 在 com.tutorialspoint 包中创建 Java 类 HelloWorld、CStartEventHandler、CStopEventHandler 和 MainApp。 |
48+ | 4 | 在 ** src** 文件夹中创建 Bean 的配置文件 Beans.xml。 |
49+ | 5 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。 |
5050
51- 这里是 ** HelloWorld.java** 文件的内容:
51+ 这里是 ** HelloWorld.java** 文件的内容:
5252
5353```
5454package com.tutorialspoint;
@@ -63,7 +63,7 @@ public class HelloWorld {
6363}
6464```
6565
66- 下面是 ** CStartEventHandler.java** 文件的内容:
66+ 下面是 ** CStartEventHandler.java** 文件的内容:
6767
6868```
6969package com.tutorialspoint;
@@ -77,7 +77,7 @@ public class CStartEventHandler
7777}
7878```
7979
80- 下面是 ** CStopEventHandler.java** 文件的内容:
80+ 下面是 ** CStopEventHandler.java** 文件的内容:
8181
8282```
8383package com.tutorialspoint;
@@ -91,7 +91,7 @@ public class CStopEventHandler
9191}
9292```
9393
94- 下面是 ** MainApp.java** 文件的内容:
94+ 下面是 ** MainApp.java** 文件的内容:
9595
9696```
9797package com.tutorialspoint;
@@ -117,7 +117,7 @@ public class MainApp {
117117}
118118```
119119
120- 下面是配置文件 ** Beans.xml** 文件:
120+ 下面是配置文件 ** Beans.xml** 文件:
121121
122122```
123123<?xml version="1.0" encoding="UTF-8"?>
@@ -140,7 +140,7 @@ public class MainApp {
140140</beans>
141141```
142142
143- 一旦你完成了创建源和 bean 的配置文件,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下消息:
143+ 一旦你完成了创建源和 bean 的配置文件,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下消息:
144144
145145```
146146ContextStartedEvent Received
@@ -154,21 +154,21 @@ ContextStoppedEvent Received
154154
155155
156156
157- ## Spring 中的自定义事件
157+ ## Spring 中的自定义事件
158158
159- 编写和发布自己的自定义事件有许多步骤。按照在这一章给出的说明来编写,发布和处理自定义 Spring 事件。
159+ 编写和发布自己的自定义事件有许多步骤。按照在这一章给出的说明来编写,发布和处理自定义 Spring 事件。
160160
161- | 步骤 | 描述 |
161+ | 步骤 | 描述 |
162162| --- | --- |
163- | 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 ** src** 文件夹中创建一个包 com.tutorialspoint。 |
164- | 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 |
165- | 3 | 通过扩展 ** ApplicationEvent** ,创建一个事件类 CustomEvent。这个类必须定义一个默认的构造函数,它应该从 ApplicationEvent 类中继承的构造函数。 |
166- | 4 | 一旦定义事件类,你可以从任何类中发布它,假定 EventClassPublisher 实现了 ApplicationEventPublisherAware。你还需要在 XML 配置文件中声明这个类作为一个 bean,之所以容器可以识别 bean 作为事件发布者,是因为它实现了 ApplicationEventPublisherAware 接口。 |
167- | 5 | 发布的事件可以在一个类中被处理,假定 EventClassHandler 实现了 ApplicationListener 接口,而且实现了自定义事件的 onApplicationEvent 方法。 |
168- | 6 | 在 ** src** 文件夹中创建 bean 的配置文件 Beans.xml 和 MainApp 类,它可以作为一个 Spring 应用程序来运行。 |
169- | 7 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。 |
163+ | 1 | 创建一个名称为 SpringExample 的项目,并且在创建项目的 ** src** 文件夹中创建一个包 com.tutorialspoint。 |
164+ | 2 | 使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。 |
165+ | 3 | 通过扩展 ** ApplicationEvent** ,创建一个事件类 CustomEvent。这个类必须定义一个默认的构造函数,它应该从 ApplicationEvent 类中继承的构造函数。 |
166+ | 4 | 一旦定义事件类,你可以从任何类中发布它,假定 EventClassPublisher 实现了 ApplicationEventPublisherAware。你还需要在 XML 配置文件中声明这个类作为一个 bean,之所以容器可以识别 bean 作为事件发布者,是因为它实现了 ApplicationEventPublisherAware 接口。 |
167+ | 5 | 发布的事件可以在一个类中被处理,假定 EventClassHandler 实现了 ApplicationListener 接口,而且实现了自定义事件的 onApplicationEvent 方法。 |
168+ | 6 | 在 ** src** 文件夹中创建 bean 的配置文件 Beans.xml 和 MainApp 类,它可以作为一个 Spring 应用程序来运行。 |
169+ | 7 | 最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。 |
170170
171- 这个是 ** CustomEvent.java** 文件的内容:
171+ 这个是 ** CustomEvent.java** 文件的内容:
172172
173173```
174174package com.tutorialspoint;
@@ -184,7 +184,7 @@ public class CustomEvent extends ApplicationEvent{
184184
185185```
186186
187- 下面是 ** CustomEventPublisher.java** 文件的内容:
187+ 下面是 ** CustomEventPublisher.java** 文件的内容:
188188
189189```
190190package com.tutorialspoint;
@@ -204,7 +204,7 @@ public class CustomEventPublisher
204204}
205205```
206206
207- 下面是 ** CustomEventHandler.java** 文件的内容:
207+ 下面是 ** CustomEventHandler.java** 文件的内容:
208208
209209```
210210package com.tutorialspoint;
@@ -217,7 +217,7 @@ public class CustomEventHandler
217217}
218218```
219219
220- 下面是 ** MainApp.java** 文件的内容:
220+ 下面是 ** MainApp.java** 文件的内容:
221221
222222```
223223package com.tutorialspoint;
@@ -235,7 +235,7 @@ public class MainApp {
235235}
236236```
237237
238- 下面是配置文件 ** Beans.xml** :
238+ 下面是配置文件 ** Beans.xml** :
239239
240240```
241241<?xml version="1.0" encoding="UTF-8"?>
@@ -254,14 +254,14 @@ public class MainApp {
254254</beans>
255255```
256256
257- 一旦你完成了创建源和 bean 的配置文件后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:
257+ 一旦你完成了创建源和 bean 的配置文件后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:
258258
259259```
260260My Custom Event
261261My Custom Event
262262```
263263
264- # 参考文章
264+ # 参考文章
265265https://www.w3cschool.cn/wkspring
266266https://www.runoob.com/w3cnote/basic-knowledge-summary-of-spring.html
267267http://codepub.cn/2015/06/21/Basic-knowledge-summary-of-Spring
0 commit comments