您在所有章节中都看到Spring的核心是 ApplicationContext ,它管理bean的完整生命周期. ApplicationContext在加载bean时发布某些类型的事件.例如,在上下文启动时发布 ContextStartedEvent ,并在上下文停止时发布 ContextStoppedEvent .
事件处理 ApplicationContext 是通过 ApplicationEvent 类和 ApplicationListener 接口提供的.因此,如果bean实现了 ApplicationListener ,那么每次将 ApplicationEvent 发布到ApplicationContext时,都会通知该bean.
Spring提供以下标准事件 :
Sr.No. | Spring内置事件&描述 |
---|---|
1 | ContextRefreshedEvent 当 ApplicationContext 初始化或刷新时,将发布此事件.这也可以使用 ConfigurableApplicationContext 接口上的refresh()方法引发. |
2 | ContextStartedEvent 使用 ConfigurableApplicationContext 接口上的start()方法启动 ApplicationContext 时,将发布此事件.您可以轮询数据库,也可以在收到此事件后重新启动任何已停止的应用程序. |
3 | ContextStoppedEvent 此事件在使用 ConfigurableApplicationContext 接口上的stop()方法停止 ApplicationContext .收到此活动后,您可以进行必要的保养工作. |
4 | ContextClosedEvent 此事件在 ApplicationContext >时发布使用 ConfigurableApplicationContext 接口上的close()方法关闭i>.封闭的环境达到了生命的终点;它无法刷新或重新启动. |
5 | RequestHandledEvent 这是一个特定于Web的事件,告诉所有bean已发出HTTP请求服务. |
Spring的事件处理是单线程的,所以如果发布了一个事件,除非所有接收者都收到消息,否则流程将被阻止,流程将无法继续.因此,如果要使用事件处理,在设计应用程序时应该小心.
监听上下文事件
收听上下文事件,bean应该实现 ApplicationListener 接口,该接口只有一个方法 onApplicationEvent().因此,让我们编写一个示例来查看事件如何传播以及如何根据特定事件将代码放到执行所需任务中.
让我们使用一个可用的Eclipse IDE并使用以下步骤创建一个Spring应用程序 :
步骤 | 描述 |
---|---|
1 | 创建一个名为 SpringExample 的项目并创建一个包 com.it1352在创建的项目中的 src 文件夹下. |
2 | 使用添加外部JAR 选项添加所需的Spring库,如 Spring Hello World示例章节中所述. |
3 | 创建Java类 HelloWorld , CStartEventHandler , CStopEventHandler 和 MainApp 在 com.it1352包下. |
4 | 在 src 文件夹下创建Beans配置文件 Beans.xml . |
5 | 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述. |
这是内容 HelloWorld.java 文件
package com.it1352; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); }}
以下是 CStartEventHandler.java 文件的内容
package com.it1352; import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextStartedEvent;public class CStartEventHandler implements ApplicationListener{ public void onApplicationEvent(ContextStartedEvent event) { System.out.println("ContextStartedEvent Received"); }}
以下是 CStopEventHandler.java 文件的内容
package com.it1352; import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextStartedEvent;public class CStartEventHandler implements ApplicationListener{ public void onApplicationEvent(ContextStartedEvent event) { System.out.println("ContextStartedEvent Received"); }}
以下是 MainApp.java 文件的内容
package com.it1352; import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); // Let us raise a start event. context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); // Let us raise a stop event. context.stop(); }}
以下是配置文件 Beans.xml
完成源和bean配置文件的创建后,让我们运行应用程序.如果您的应用程序一切正常,它将打印以下消息 :
ContextStartedEvent ReceivedYour Message : Hello World!ContextStoppedEvent Received
如果您愿意,可以发布自己的自定义事件,稍后您可以捕获相同的事件以对这些自定义事件采取任何操作.如果您有兴趣编写自己的自定义事件,可以查看 Spring中的自定义事件.