课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 中的自定义事件

当前位置:免费教程 » Java相关 » Spring

编写和发布自己的自定义事件有许多步骤。按照在这一章给出的说明来编写,发布和处理自定义 Spring 事件。

步骤描述
1创建一个名称为 SpringExample 的项目,并且在创建项目的 src 文件夹中创建一个包 com.tutorialspoint。
2使用 Add External JARs 选项,添加所需的 Spring 库,解释见 Spring Hello World Example 章节。
3通过扩展 ApplicationEvent,创建一个事件类 CustomEvent。这个类必须定义一个默认的构造函数,它应该从 ApplicationEvent 类中继承的构造函数。
4一旦定义事件类,你可以从任何类中发布它,假定 EventClassPublisher 实现了 ApplicationEventPublisherAware。你还需要在 XML 配置文件中声明这个类作为一个 bean,之所以容器可以识别 bean 作为事件发布者,是因为它实现了 ApplicationEventPublisherAware 接口。
5发布的事件可以在一个类中被处理,假定 EventClassHandler 实现了 ApplicationListener 接口,而且实现了自定义事件的 onApplicationEvent 方法。
6src 文件夹中创建 bean 的配置文件 Beans.xml 和 MainApp 类,它可以作为一个 Spring 应用程序来运行。
7最后一步是创建的所有 Java 文件和 Bean 配置文件的内容,并运行应用程序,解释如下所示。

这个是 CustomEvent.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationEvent;
  3. public class CustomEvent extends ApplicationEvent{
  4. public CustomEvent(Object source) {
  5. super(source);
  6. }
  7. public String toString(){
  8. return "My Custom Event";
  9. }
  10. }

下面是 CustomEventPublisher.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationEventPublisher;
  3. import org.springframework.context.ApplicationEventPublisherAware;
  4. public class CustomEventPublisher
  5. implements ApplicationEventPublisherAware {
  6. private ApplicationEventPublisher publisher;
  7. public void setApplicationEventPublisher
  8. (ApplicationEventPublisher publisher){
  9. this.publisher = publisher;
  10. }
  11. public void publish() {
  12. CustomEvent ce = new CustomEvent(this);
  13. publisher.publishEvent(ce);
  14. }
  15. }

下面是 CustomEventHandler.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationListener;
  3. public class CustomEventHandler
  4. implements ApplicationListener<CustomEvent>{
  5. public void onApplicationEvent(CustomEvent event) {
  6. System.out.println(event.toString());
  7. }
  8. }

下面是 MainApp.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.context.ConfigurableApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ConfigurableApplicationContext context =
  7. new ClassPathXmlApplicationContext("Beans.xml");
  8. CustomEventPublisher cvp =
  9. (CustomEventPublisher) context.getBean("customEventPublisher");
  10. cvp.publish();
  11. cvp.publish();
  12. }
  13. }

下面是配置文件 Beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <bean id="customEventHandler"
  7. class="com.tutorialspoint.CustomEventHandler"/>
  8. <bean id="customEventPublisher"
  9. class="com.tutorialspoint.CustomEventPublisher"/>
  10. </beans>

一旦你完成了创建源和 bean 的配置文件后,我们就可以运行该应用程序。如果你的应用程序一切都正常,将输出以下信息:

  1. My Custom Event
  2. My Custom Event
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号