Spring事件处理
你已经看到了所有的章节了Spring的核心是ApplicationContext,它管理bean的整个生命周期。ApplicationContext中加载bean时发布某些类型的事件。例如,一个ContextStartedEvent是当所述上下文被启动并发布,上下文被停止ContextStoppedEvent。
事件处理中ApplicationContext 通过了ApplicationEvent类和ApplicationListener接口提供。所以,如果一个bean实现了ApplicationListener,然后每anApplicationEvent被发布到ApplicationContext的时候通知bean。
Spring提供了以下标准的事件:
S.N. | Spring 内置活动及说明 |
---|---|
1 | ContextRefreshedEvent This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface. |
2 | ContextStartedEvent This event is published when the ApplicationContext is started using the start() method on theConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event. |
3 | ContextStoppedEvent This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event. |
4 | ContextClosedEvent This event is published when the ApplicationContext is closed using the close() method on theConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted. |
5 | RequestHandledEvent This is a web-specific event telling all beans that an HTTP request has been serviced. |
Spring事件处理是单线程的,所以如果一个事件被发布,直至及除非所有的接收器得到的消息,该进程被阻塞并且流程将不会继续。因此,应注意在设计应用程序时,如果事件处理被使用。
监听上下文事件:
要监听上下文事件,一个bean应该实现ApplicationListener接口,只有一个方法onApplicationEvent()。因此,让我们写一个例子,看看如何传播的事件,以及如何可以把代码来执行基于某些事件所需的任务。
让我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:
步骤 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld, CStartEventHandler, CStopEventHandler and MainAppunder the com.manongjc package. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
这里是HelloWorld.java 文件的内容:
package com.manongjc;
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.manongjc;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
以下是CStopEventHandler.java文件的内容:
package com.manongjc;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent;
public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{
public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
以下是MainApp.java文件的内容:
package com.manongjc;
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文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.manongjc.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
<bean id="cStartEventHandler"
class="com.manongjc.CStartEventHandler"/>
<bean id="cStopEventHandler"
class="com.manongjc.CStopEventHandler"/>
</beans>
创建源程序和bean配置文件完成,让我们运行应用程序。如果一切顺利,这将打印以下信息:
ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received
如果喜欢,你可以发布自己的自定义事件,以后你可以捕捉相同处理那些自定义事件动作。如果你有兴趣在编写自己的自定义事件,可以查看 Spring自定义事件