第十八章 Spring中定制事件(Spring Framework3.1教程)
2014-03-09 17:24
323 查看
发布自己的事件有许多步骤要编写。按照本章给的说明去编写、发布并处理Spring定制事件。
如下是CustomEvent类源代码:
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source) {
super(source);
}
public String toString() {
return "My Custom Event";
}
}
如下是CustomEventPublisher类源代码:
如下是CustomEventHandler类源代码:
如下是MainApp类源代码:
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp18 {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans18.xml");
CustomEventPublisher cvp = (CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
一旦你完成了源代码和配置文件的创建,运行这个应用。如果应用一切正常,将会打印如下消息:
步骤 | 描述 |
1 | 创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。所有的类都在这个包下。 |
2 | 在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。 |
3 | 创建一个事件类,定制类通过扩展ApplicationEvent。这个类必须定义一个默认的构造函数且继承ApplicationEvent类的构造函数。 |
4 | 一旦你定义了事件类,你可以从任何类中发布,我们说的事件发布类实现了ApplicationEventPublisherAware接口。你同样需要在XML配置文件中声明这个类,这样容器可以确定这个类是一个事件发布者因为它继承了ApplicationEventPublisherAware接口。 |
5 | 一个发布的事件可以在类中处理,我们说的事件处理类实现了ApplicationListener接口并且实现了onApplicationEvent方法对这个定制的事件。 |
6 | 在src目录下创建一个bean配置文件Beans.xml和MainApp类,作为一个Spring应用。 |
7 | 最后一步在Java类中和Bean配置文件中添加内容,并运行应用。 |
package com.tutorialspoint;
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source) {
super(source);
}
public String toString() {
return "My Custom Event";
}
}
如下是CustomEventPublisher类源代码:
package com.tutorialspoint; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; public class CustomEventPublisher implements ApplicationEventPublisherAware { private ApplicationEventPublisher publisher; public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } public void publish() { CustomEvent ce = new CustomEvent(this); publisher.publishEvent(ce); } }
如下是CustomEventHandler类源代码:
package com.tutorialspoint; import org.springframework.context.ApplicationListener; public class CustomEventHandler implements ApplicationListener<CustomEvent> { public void onApplicationEvent(CustomEvent event) { System.out.println(event.toString()); } }
如下是MainApp类源代码:
package com.tutorialspoint;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp18 {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans18.xml");
CustomEventPublisher cvp = (CustomEventPublisher) context.getBean("customEventPublisher");
cvp.publish();
cvp.publish();
}
}
一旦你完成了源代码和配置文件的创建,运行这个应用。如果应用一切正常,将会打印如下消息:
My Custom Event My Custom Event
相关文章推荐
- 第十七章 Spring 事件处理(Spring Framework3.1教程)
- spring发布和接收定制的事件(spring事件传播)
- spring学习教程8-自定义属性编辑器PropertyEditor 和自定义事件
- 第三章 Spring环境设置(Spring Framework3.1教程)
- spring发布和接收定制的事件(spring事件传播)
- 第十三章 Spring注入集合(Spring Framework3.1教程)
- Android基础入门教程——3.1 基于监听的事件处理机制
- 第四章 Spring Hello World示例 (Spring Framework3.1教程)
- 第十四章 Spring Bean自动注入(Spring Framework3.1教程)
- 第十五章 Spring基于注解的配置(Spring Framework3.1教程)
- 第十六章 Spring基于Java的配置(Spring Framework3.1教程)
- spring发布和接收定制的事件(spring事件传播)
- spring发布和接收定制的事件(spring事件传播)
- 第十一章 Spring依赖注入(Spring Framework3.1教程)
- 第五章 Spring IoC容器(Spring Framework3.1教程)
- spring发布和接收定制的事件(spring事件传播)
- 第十二章 Spring内部Bean注入(Spring Framework3.1教程)
- spring发布和接收定制的事件(spring事件传播)
- 【Android 开发教程】触发事件
- Spring Boot (教程八: 过滤器、监听器、拦截器)