第十七章 Spring 事件处理(Spring Framework3.1教程)

你已经看到了所有的Spring核心章节ApplicationContext,它管理所有bean的整个声明周期。ApplicationContext发布确定类型的事件在加载类时。例如一个ContextStarterEvent被发布当上下文启动时,ContextStoppedEvent被发布当上下文结束时。

         ApplicationContext中的事件处理通过ApplicationEvent类和ApplicationListener接口发布。因此如果一个bean继承了ApplicationListener,每次一个ApplicationEvent被发布到ApplicationContext,这个bean将得到通知。

Spring提供了如下标准的事件:

序号 

Spring内置事件&描述

1

ContextRefreshedEvent

这个事件被发布当Application Context被初始化或者刷新时,它可以由refresh()方法引起在ConfigurableApplicationtext接口中。

2

ContextStartedEvent

这个事件被发布当ApplicationContext通过ConfiugrableApplication()接口的start()方法启动时。你可以查询数据库或者你可以重新启动任何停止的应用在收到这个事件后。

3

ContextStoppedEvent

这个事件被发布当ApplicationContext通过ConfigurableApplication接口的stop()方法停止时。你可以做必须的看家工作在收到这个事件时。

4

ContextClosedEvent

这个事件被发布当ApplicationContext通过ConfigurableApplicationContext接口的close()方法关闭时。一个关闭的上下文到达它声明的终点,不能在被刷新或者重启。

5

RequestHandledEvent

 这是一个web专有的事件,告诉所有bean有一个HTTP请求已经被处理。

Spring的事件处理是单线程的,因此如果一个事件被发布、直到除非收到所有的消息,否则处理将阻塞并且流程将不会继续。因此,设计你的应用应该小心如果事件处理被使用。

监听Context Events

监听一个上下文事件,bean需要继承ApplicationListener接口它只有一个方法onApplicationEvent()。因此让我们写一个例子看看事件如何传播及如何放置你的代码处理需要的任务基于特定的事件。

让我们使用Eclipse IDE按如下的步骤创建一个Spring应用:

步骤 

描述

1

创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。

2

在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。

3

在com.tutorialspoint包下创建 HelloWorld、 CStartEventHandler、CStopEventHandler 、 MainApp类。

4

在scr目录下创建配置文件Beans.xml

5

最后一步在Java类中和Bean配置文件中添加内容,并运行应用。

如下是HelloWorld的源代码:

package com.tutorialspoint;
public class HelloWorld {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public void getMessage() {
        System.out.println("Your Message : " + message);
    }
}

如下是CStartEventHandler源代码:

package com.tutorialspoint;
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:

package com.tutorialspoint;

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的源代码:

package com.tutorialspoint;

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.tutorialspoint.HelloWorld">
		<property name="message" value="Hello World!" />
	</bean>

	<bean id="cStartEventHandler" class="com.tutorialspoint.CStartEventHandler" />

	<bean id="cStopEventHandler" class="com.tutorialspoint.CStopEventHandler" />

</beans>

一旦你完成了源代码和配置文件的创建,运行这个应用。如果应用一切正常,将会打印如下消息:

ContextStartedEvent Received
Your Message : Hello World!
ContextStoppedEvent Received

如果你喜欢,你可以发布你自己定制的事件,你可以俘获它然后做一些事情处理定制的事件。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值