Spring(十)之自定义事件

编写自定义事件的简单流程如下:

(1)编写CustomEvent.java

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";
   }
}

(2)编写CustomEventPublisher.java

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);
   }
}

(3)编写CustomEventHandler.java

package com.tutorialspoint;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler implements ApplicationListener<CustomEvent> {
   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }
}

(4)编写MainApp.java

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");

      CustomEventPublisher cvp =
         (CustomEventPublisher) context.getBean("customEventPublisher");

      cvp.publish();
      cvp.publish();
   }
}

(5)编写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 = "customEventHandler" class = "com.tutorialspoint.CustomEventHandler"/>
   <bean id = "customEventPublisher" class = "com.tutorialspoint.CustomEventPublisher"/>

</beans>

(6)运行MainApp.java中的main方法

原文地址:https://www.cnblogs.com/youcong/p/9460716.html

时间: 2024-07-30 22:27:27

Spring(十)之自定义事件的相关文章

深入理解Spring的容器内事件发布监听机制

目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监听 4.Spring事件监听源码解析 4.1 初始化事件发布器流程 4.2 注册事件监听器流程 4.3 容器事件发布流程 5.总结 1. 什么是事件监听机制 在讲解事件监听机制前,我们先回顾下设计模式中的观察者模式,因为事件监听机制可以说是在典型观察者模式基础上的进一步抽象和改进.我们可以在JDK或

Spring 事件(2)- 自定义事件

Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Control – IOC) 理解依赖注入(DI – Dependency Injection) Bean XML 配置(1)- 通过XML配置加载Bean Bean XML 配置(2)- Bean作用域与生命周期回调方法配置 Bean XML 配置(3)- 依赖注入配置 Bean XML 配置(

Spring内置事件以及自定义事件

1. Spring内置的事件有哪些? Spring中的事件是一个 ApplicationEvent类的子类,由实现 ApplicationEventPublisherAware 接口的类发送,实现 ApplicationListener 接口的类监听. Spring中已经定义了一组内置事件,这些事件由ApplicationContext容器发出.(ContextRefreshedEvent.ContextStartedEvent.ContextStoppedEvent.ContextClosed

Java Spring 自定义事件监听

ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextStartedEvent; public class EventStart implements ApplicationListener<ContextStartedEvent>{ @Override pub

第四十四课、发送自定义事件(下)

一.自定义事件对象 1.Qt可以自定义新的事件类 (1).自定义的事件类必须继承自QEvent (2).自定义的事件类必须拥有全局唯一的Type值(event->type()可以获得事件的类型) (3).程序中必须提供处理自定义事件的方法 2.自定义事件类 (1).将QEvent作为父类继承 (2).指定全局唯一的Type值 3.Qt事件的Type值 (1).每个事件类都拥有全局唯一的Type值(类似于唯一的ID号) (2).自定义事件类的Type值也需要自定义 (3).自定义事件类使用QEve

spring发布和接收事件

EventObject,为JavaSE提供的事件类型基类,任何自定义的事件都继承自该类,例如上图中右侧灰色的各个事件.Spring中提供了该接口的子类ApplicationEvent. EventListener为JavaSE提供的事件监听者接口,任何自定义的事件监听者都实现了该接口,如上图左侧的各个事件监听者.Spring中提供了该接口的子类ApplicationListener接口. JavaSE中未提供事件发布者这一角色类,由各个应用程序自行实现事件发布者这一角色.Spring中提供了Ap

Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)

一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要让另一个Bean监听当前Bean所发送的事情. Spring的事件需要遵循如下流程: (1)自定义事件,集成ApplicationEvent. (2)定义事件监听器,实现ApplicationListener. (3)使用容器发布事件. 示例: 1.自定义事件. package com.ecwork

【2016-11-11】【坚持学习】【Day24】【WPF 自定义控件 附加属性 自定义事件】

UserControl ,自定义控件. 这里刚刚想到一个问题.什么时候应该用usercontrol 定义一个控件.什么时候应该重写控件的template和样式,实现新效果. 引用一下人家的话:http://www.cnblogs.com/denghejun/p/3671061.html 我的理解: Usercontrol应该是一个带有功能,带有行为的控件.而一些简单的模型,样式效果,应该都可以用Template实现.它大部分是给呈现效果服务,当然,它可以带有很多事件触发器. 另外,在一些复杂的功

jQuery的自定义事件——滚轮

这个案例类似于在地图上滚动滚轮,能缩小或者放大地图,分别用zoomIn和zoomOut来命名. 代码如下: //JS部分:<script src="jquery-1.10.2.min.js"></script> <script type="text/javascript"> $(function(){ $('img').on('zoomIn', function(){ $(this).css('width', 300) }); $