Spring Application Event Example
Spring Application Event
项目结构
工程下载
https://github.com/xiaoheike/SpringApplicationEventExample.git
SportEvent
package nd.esp.com.event;
import org.springframework.context.ApplicationEvent;
public class SportEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
public SportEvent(Object source) {
super(source);
}
}
SportEvent
是所有体育运动的父类,继承Spring ApplicationEvent
,必须实现带参数构造函数,如上述代码所示。带参数构造函数传入参数可用于携带其他的信息。具体在NBAEvent
中可以看到作用。
NBAEvent
package nd.esp.com.event;
public class NBAEvent extends SportEvent {
private static final long serialVersionUID = 1L;
private String text;
public NBAEvent(Object source) {
super(source);
}
public NBAEvent(String source, String text) {
super(source);
this.text = text;
}
public String news() {
return text;
}
@Override
public String getSource() {
return String.class.cast(super.getSource());
}
}
NBAEvent
主题,用于显示NBA相关信息,上述getSource()
方法重写了Spring ApplicationEvent 的父类EventObject
,可以用于获得构造函数传入的参数。
SoccerEvent
package nd.esp.com.event;
public class SoccerEvent extends SportEvent {
private static final long serialVersionUID = 1L;
private String text;
public SoccerEvent(String source) {
super(source);
}
public SoccerEvent(Object source, String text) {
super(source);
this.text = text;
}
public String news() {
return text;
}
@Override
public String getSource() {
return String.class.cast(super.getSource());
}
}
SoccerEvent
主题,用于显示足球先关信息,和NBAEvent
功能类似。
SportEventListener
SportEventListener1
package nd.esp.com.listener;
import nd.esp.com.event.NBAEvent;
import nd.esp.com.event.SoccerEvent;
import nd.esp.com.event.SportEvent;
import org.springframework.context.ApplicationListener;
public class SportEventListener implements ApplicationListener<SportEvent> {
public void onApplicationEvent(SportEvent event) {
if (event instanceof NBAEvent) {
System.out.println("SportEventListener1:" + NBAEvent.class.cast(event).getSource());
}
if (event instanceof SoccerEvent) {
SoccerEvent soccerEvent = SoccerEvent.class.cast(event);
System.out.println("SportEventListener1:" + soccerEvent.getSource() + " " + soccerEvent.news());
}
}
}
SportEventListener2
package nd.esp.com.listener;
import nd.esp.com.event.NBAEvent;
import nd.esp.com.event.SoccerEvent;
import nd.esp.com.event.SportEvent;
import org.springframework.context.ApplicationListener;
public class SportEventListener2 implements ApplicationListener<SportEvent> {
public void onApplicationEvent(SportEvent event) {
if (event instanceof NBAEvent) {
System.out.println("SportEventListener2:" + NBAEvent.class.cast(event).getSource());
}
if (event instanceof SoccerEvent) {
SoccerEvent soccerEvent = SoccerEvent.class.cast(event);
System.out.println("SportEventListener2:" + soccerEvent.getSource() + " " + soccerEvent.news());
}
}
}
SportEventListener
是观察者,当有新事件到来会被调用。SportEvent
的功能在这里得到体现,通过创建父类,可以同时监控一类相似的主题,比如NBA事件以及足球事件。这样可以少些监听者。可以有多个观察者监听相同的时间,这里是指SportEvent。
SportEventPublisher
package nd.esp.com.publisher;
import nd.esp.com.event.NBAEvent;
import nd.esp.com.event.SoccerEvent;
import nd.esp.com.event.SportEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SportEventPublisher implements ApplicationContextAware {
private ApplicationContext applicationEventPulisher = null;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationEventPulisher = applicationContext;
}
public void publishEvent(SportEvent sportEvent) {
applicationEventPulisher.publishEvent(sportEvent);
}
public static void main(String[] args) {
String[] xmlConfig = new String[] { "applicationContext.xml" };
// 使用ApplicationContext来初始化系统
ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig);
SportEventPublisher publisher = (SportEventPublisher) context.getBean("applicationContextAware");
publisher.publishEvent(new NBAEvent("NBA sport:骑士队获得总冠军"));
publisher.publishEvent(new SoccerEvent("Scorrer sport:标题:中国获得世界杯冠军", "中国击败世界各国,取得10连冠"));
}
}
SportEventPublisher
当有新的时间时,由它发布,这个类继承Spring ApplicationContextAware
,能够自动注入ApplicationContext
对象。这个类实现了主题的通知功能。
appicationContext.xml(单线程)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="applicationContextAware" class="nd.esp.com.publisher.SportEventPublisher"></bean>
<bean id="applicationListener1" class="nd.esp.com.listener.SportEventListener1"></bean>
<bean id="applicationListener2" class="nd.esp.com.listener.SportEventListener2"></bean>
</beans>
这两个bean是必须的,id可以任意,spring针对SportEventPublisher
肯定是有经过特殊处理的,要不然该类中的ApplicationEventPublisher
无法注入对应类。
这样的配置默认是采用单线程,为了提高效率可以使用多线程。修改application.xml
文件如下即可。
applicationContext.xml(多线程)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="applicationContextAware" class="nd.esp.com.publisher.SportEventPublisher"></bean>
<bean id="applicationListener1" class="nd.esp.com.listener.SportEventListener1"></bean>
<bean id="applicationListener2" class="nd.esp.com.listener.SportEventListener2"></bean>
<bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="keepAliveSeconds" value="30000" />
<property name="maxPoolSize" value="1000" />
<property name="queueCapacity" value="200" />
</bean>
<bean id="applicationEventMulticaster"
class="org.springframework.context.event.SimpleApplicationEventMulticaster">
<property name="taskExecutor" ref="taskExecutor" />
</bean>
</beans>
idapplicationEventMulticaster
不能够改变,猜测是spring需要根据这个id注入对象。
SimpleApplicationEventMulticaster
多线程或者单线程可以通过该类中的方法
@SuppressWarnings("unchecked")
public void multicastEvent(final ApplicationEvent event) {
for (final ApplicationListener listener : getApplicationListeners(event)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
public void run() {
listener.onApplicationEvent(event);
}
});
}
else {
listener.onApplicationEvent(event);
}
}
}
多线程产经下变量executor
的值不为空,单线程则相反。
运行结果##
教程结束,感谢阅读。
欢迎转载,但请注明本文链接,谢谢。
2016.5.12 17:49
Spring Application Event Example的更多相关文章
- Spring 事件:Application Event
Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- Spring Session event事件分析
1. org.apache.catalina.session.StandardSession 这是servlet-api jar包中的一个类.是session接口的标准实现.当session创建的时候 ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- Spring应用事件(Application Event)
Spring的事件为Bean与Bean的消息通信提供的支持.当一个Bean处理完了一个任务以后,希望另一个Bean知道并能做出相应的处理,这是我们就需要让另一个Bean监听当前Bean所发送的事件. ...
- 事件(Application Event)
Spring的事件(Appllcation Event)为Bean与Bean之间的消息通信提供了支持.当一个Bean处理完一个任务后,希望另一个Bean知道并能做相应的处理,这种情况可以让另一个Bea ...
随机推荐
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
- 成为一名优秀的Web前端开发者
本文记录了两位工程师为web开发者们所提出的多条建议,其中一位推荐了多种实用的工具与技术,而另一位则对于如何克服浏览器开发时所面临的挑战提出了诸多建议. Rebecca Murphey是来自于Baza ...
- 苹果公布WWDC2016时间 并做了个程序员情怀网页
新浪手机讯 4月19日上午消息,苹果公司今日正式确定2016年全球开发者大会(WWDC)开幕时间:6月13-17日,并做了个非常有意思的代码风格页面. 网友戏称这个页面只有程序员们才能看懂,它的首页是 ...
- 第二篇:Retrofit调用流程图和使用到的设计模式
2016-05-08 09:35:58 这篇文章解析一下Retrofit的调用流程 1. 先看一下我们是如何使用Retrofit的,代码如下: public interface WeatherData ...
- idea 中利用maven创建java web 项目
转自:http://www.linuxidc.com/Linux/2014-04/99687.htm 本文主要使用图解介绍了使用IntelliJ IDEA 12创建Maven管理的Java Web项目 ...
- Swift运算符
运算符分类 运算符分类 一元运算符 1.负号运算符 var number1 = var number2 = -number1 2.正号运算符 //正号运算符不做任何操作 var number3 = + ...
- eclipse + python dev
错误:Project interpreter not specified解决方法 http://blog.csdn.net/magictong/article/details/7288732 安装Py ...
- WPF学习笔记1---初接触
刚刚接触WPF,微软的一套东西.WPF最大的特点就是UI设计与代码逻辑的完全剥离.这样美工和程序员的分工就变得非常清楚.因为界面和程序的耦合度很低,也增加的代码的灵活性和可重用性. 微软为WPF的UI ...
- Java泛型学习笔记 - (一)泛型的介绍
一.什么是泛型:泛型的作用是用来规定一个类, 接口或方法所能接受的数据的类型. 就像在声明方法时指定参数一样, 我们在声明一个类, 接口或方法时, 也可以指定其"类型参数", 也就 ...
- js④
for循环代码执行顺序 1.执行小括号里面的第一个语句 2.判断小括号里面第二个语句的布尔值,如果为false,就会结束掉整个for循环,如果为true,就会执行大括号里面的语句块; 3.每次执行完大 ...