以下內容引用自http://wiki.jikexueyuan.com/project/spring/event-handling-in-spring.html:
Spring的核心是ApplicationContext,它負責管理beans的完整生命周期。當加載beans時,ApplicationContext發布某些類型的事件。例如,當上下文啟動時,ContextStartedEvent事件觸發,當上下文停止時,ContextStoppedEvent事件觸發。
通過ApplicationEvent類和ApplicationListener接口來提供在ApplicationContext中處理事件。如果一個bean實現ApplicationListener,那么每次ApplicationEvent被發布到ApplicationContext上時,那個bean會被通知。
Spring提供了以下的標准事件:
Spring 內置事件 | 描述 |
---|---|
ContextRefreshedEvent | ApplicationContext被初始化或刷新時,該事件被發布。這也可以在ConfigurableApplicationContext接口中使用refresh()方法來發生。 |
ContextStartedEvent | 當使用ConfigurableApplicationContext接口中的start()方法啟動ApplicationContext時,該事件被觸發。你可以查詢你的數據庫,或者你可以在接受到這個事件后重啟任何停止的應用程序。 |
ContextStoppedEvent | 當使用ConfigurableApplicationContext接口中的stop()方法停止ApplicationContext時,該事件被觸發。你可以在接受到這個事件后做必要的清理的工作。 |
ContextClosedEvent | 當使用ConfigurableApplicationContext接口中的close()方法關閉ApplicationContext時,該事件被觸發。一個已關閉的上下文到達生命周期末端;它不能被刷新或重啟。 |
RequestHandledEvent | 這是一個web-specific事件,告訴所有bean HTTP請求已經被服務。 |
Spring的事件處理是單線程的,所以如果一個事件被觸發,除非所有的接收者得到消息,否則這些進程被阻止,流程將不會繼續。因此,如果要使用事件處理,在設計應用程序時應小心。
監聽上下文事件
為了監聽上下文事件,一個bean應該實現只有一個方法onApplicationEvent()的ApplicationListener接口。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId> <artifactId>testeventhandling</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testeventhandling</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
HelloWorld.java:
package com.jsoft.testspring.testeventhandling; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println(this.message); } }
StartedEvent.java:
package com.jsoft.testspring.testeventhandling; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStartedEvent; public class StartedEvent implements ApplicationListener<ContextStartedEvent> { @Override public void onApplicationEvent(ContextStartedEvent event) { // TODO Auto-generated method stub System.out.println("StartedEvent"); } }
StoppedEvent.java:
package com.jsoft.testspring.testeventhandling; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStoppedEvent; public class StoppedEvent implements ApplicationListener<ContextStoppedEvent> { @Override public void onApplicationEvent(ContextStoppedEvent event) { // TODO Auto-generated method stub System.out.println("StoppedEvent"); } }
beas.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.xsd"> <bean id="helloWorld" class="com.jsoft.testspring.testeventhandling.HelloWorld"> <property name="message" value="Hello World!"></property> </bean> <bean id="startedEvent" class="com.jsoft.testspring.testeventhandling.StartedEvent"></bean> <bean id="stoppedEvent" class="com.jsoft.testspring.testeventhandling.StoppedEvent"></bean> </beans>
App.java:
package com.jsoft.testspring.testeventhandling; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */ public class App { public static void main( String[] args ) { ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); applicationContext.start(); HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld"); helloWorld.setMessage("Hello World!"); helloWorld.getMessage(); applicationContext.stop(); } }
測試結果:
測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test14/testeventhandling