原理:

  1. 通过扩展ApplicationEvent,创建一个事件类CustomEvent。这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数。
  2. 一旦定义事件类,你可以从任何类中发布它,假定EventClassPublisher实现了ApplicationEventPublisherAware。你还需要在XML配置文件中声明这个类作为一个bean,之所以容器可以识别bean作为事件发布者,是因为它实现了ApplicationEventPublisherAware接口。
  3. 发布的事件可以在一个类中被处理,假定EventClassHandler实现了ApplicationListener接口,而且实现了自定义事件的onApplicationEvent方法。

例子:

pom.xml:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>com.jsoft.testspring</groupId>
  6. <artifactId>testcustomevent</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>testcustomevent</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <dependencies>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>3.8.1</version>
  22. <scope>test</scope>
  23. </dependency>
  24.  
  25. <!-- Spring Core -->
  26. <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
  27. <dependency>
  28. <groupId>org.springframework</groupId>
  29. <artifactId>spring-core</artifactId>
  30. <version>4.1.4.RELEASE</version>
  31. </dependency>
  32. <!-- Spring Context -->
  33. <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-context</artifactId>
  37. <version>4.1.4.RELEASE</version>
  38. </dependency>
  39.  
  40. </dependencies>
  41. </project>

CustomEvent.java:

  1. package com.jsoft.testspring.testcustomevent;
  2.  
  3. import org.springframework.context.ApplicationEvent;
  4.  
  5. public class CustomEvent extends ApplicationEvent {
  6.  
  7. public CustomEvent(Object source) {
  8. super(source);
  9. // TODO Auto-generated constructor stub
  10. }
  11.  
  12. public String toString(){
  13. return "My Custom Event";
  14. }
  15.  
  16. }

CustomEventPublisher.java:

  1. package com.jsoft.testspring.testcustomevent;
  2.  
  3. import org.springframework.context.ApplicationEventPublisher;
  4. import org.springframework.context.ApplicationEventPublisherAware;
  5.  
  6. public class CustomEventPublisher implements ApplicationEventPublisherAware {
  7.  
  8. private ApplicationEventPublisher publisher;
  9.  
  10. @Override
  11. public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  12. // TODO Auto-generated method stub
  13. this.publisher = applicationEventPublisher;
  14. }
  15.  
  16. public void publish(){
  17. CustomEvent ce = new CustomEvent(this);
  18. publisher.publishEvent(ce);
  19. }
  20.  
  21. }

CustomEventHandler.java:

  1. package com.jsoft.testspring.testcustomevent;
  2.  
  3. import org.springframework.context.ApplicationListener;
  4.  
  5. public class CustomEventHandler implements ApplicationListener<CustomEvent> {
  6.  
  7. @Override
  8. public void onApplicationEvent(CustomEvent event) {
  9. // TODO Auto-generated method stub
  10. System.out.println(event.toString());
  11. }
  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.xsd">
  6.  
  7. <bean id="customEventHandler" class="com.jsoft.testspring.testcustomevent.CustomEventHandler"></bean>
  8.  
  9. <bean id="customEventPublisher" class="com.jsoft.testspring.testcustomevent.CustomEventPublisher"></bean>
  10. </beans>

App.java:

  1. package com.jsoft.testspring.testcustomevent;
  2.  
  3. import org.springframework.context.ConfigurableApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. /**
  7. * Hello world!
  8. *
  9. */
  10. public class App
  11. {
  12. public static void main( String[] args )
  13. {
  14. ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  15. CustomEventPublisher publisher = (CustomEventPublisher)context.getBean("customEventPublisher");
  16. publisher.publish();
  17. publisher.publish();
  18. }
  19. }

测试结果:

总结:

此实例中主要理解为:1、事件发布;2、事件监听;3、实现事件处理。

beans中主要是事件处理类的初始化和事件发布类的初始化。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test15

Spring中实现自定义事件的更多相关文章

  1. Spring中ApplicationContext对事件的支持

    Spring中ApplicationContext对事件的支持   ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...

  2. wpf自定义控件中使用自定义事件

    wpf自定义控件中使用自定义事件 1 创建自定义控件及自定义事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...

  3. 在Spring中使用异步事件实现同步事务

    结合Scala+Spring,我们将采取一个很简单的场景:下订单,然后发送一封电子邮件. 编制一个服务: @Serviceclass OrderService @Autowired() (orderD ...

  4. DragonBones龙骨骨骼中的自定义事件(另有声音、动画事件)

    参考: DragonBones骨骼动画事件系统详解 一.在DragonBones中添加自定义事件帧 动画制作时 时间轴拉到最下面有一个事件层,添加一个事件帧 左边属性面板定义自定义事件 二.Egret ...

  5. Spring(十)之自定义事件

    编写自定义事件的简单流程如下: (1)编写CustomEvent.java package com.tutorialspoint; import org.springframework.context ...

  6. spring中增加自定义配置支持

    spring.schemas 在使用spring时,我们会首先编写spring的配置文件,在配置文件中,我们除了使用基本的命名空间http://www.springframework.org/sche ...

  7. vue--组件中的自定义事件

    父组件通过props向子组件传递数据,子组件通过自定义事件向父组件传递信息. 在子组件中通过$emit触发事件,父组件在直接使用子组件的地方使用v-on(即@)来监听子组件触发的事件. 举例:(不知道 ...

  8. DOM3中的自定义事件

    DOM3级还定义了自定义事件,自定义事件不是由DOM原生触发的,它的目的是让开发人员创建自己的事件.要创建的自定义事件可以由createEvent("CustomEvent"); ...

  9. JS 中的自定义事件和模拟事件

    在 JS 中模拟事件指的是模拟 JS 中定义的一些事件,例如点击事件,键盘事件等. 自定义事件指的是创建一个自定义的,JS 中之前没有的事件. 接下来分别说一下创建这两种事件的方法. 创建自定义事件 ...

随机推荐

  1. 记一次mysql优化操作

    这次操作,起因是需要获取用户来源及用户性别,而用户的性别信息在第三方授权的中有,存为JSON格式, 不想用php去解析获取,所以试试mysql操作 如果你有更好的解决方案,请留言告诉我! 情景简化 表 ...

  2. Mybatis和Spring整合&逆向工程

    Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的appli ...

  3. System类与两种输入流

    1.System类对I/O的支持系统输出System.out.println 是利用了I/O流的模式完成的.实际是打印流PrintStream对象 System类中定义了三个操作的常量 1.标准/系统 ...

  4. MySQL存储过程实现分页及变量的定义

    delimiter是MySQL中的命令,这个命令与存储过程没什么关系. 其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了. 即改变输入结束符. 默认情况下,delimit ...

  5. PHP 中空字符串介绍0、null、empty和false之间的关系

    0是数字,是empty,是false,不是null,值相当于空字符串,但类型不是字符串,去空格或强制转换为字符串型时不等于空字符串 ""的值相当于0,是empty,是空字符串,是f ...

  6. 阿里云人脸比对API封装

    这是根据封装是根据阿里云官方给的Demo进行修改的,当时是因为编写微信小程序云函数需要使用到阿里云人脸比对接口,才对其进行封装的. 记录下来,方便下次使用. 复制下来可以直接使用. 用到的依赖如下: ...

  7. Omnidirectional DSO: Direct Sparse Odometry with Fisheye Cameras 论文摘要

    1. Abstract 通过一种Unified Omnidirectional Model作为投影方程. 这种方式可以使用图像的所有内容包括有强畸变的区域,而现存的视觉里程计方案只能修正或者切掉来使用 ...

  8. spring注解开发-声明式事务(源码)

    1. 环境搭建与测试 1)导入相关依赖 数据源.数据库驱动.Spring-jdbc模块 <dependency> <groupId>org.springframework< ...

  9. JavaScript递归简单实现个对象深拷贝

    JavaScript中对象的深拷贝来说一直都算比较恶心 毕竟没有什么api能直接全拷贝了 得自己便利写  最近在项目中需要深拷贝 自己简单封了个方法 话不多说 直接上码 <!DOCTYPE ht ...

  10. logging日志模块配置

    logging日志模块 日志级别 日志一共分成5个等级,从低到高分别是: 1)DEBUG 2)INFO 3)WARNING 4)ERROR 5)CRITICAL 说明: DEBUG:详细的信息,通常只 ...