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

(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方法

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

  1. Spring中实现自定义事件

    原理: 通过扩展ApplicationEvent,创建一个事件类CustomEvent.这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数. 一旦定义事件类, ...

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

    目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...

  3. Spring容器事件、自定义事件

    Spring容器内置事件,如容器的启动.停止.关闭.销毁等事件 <bean name="contextStartedHandler" class="com.nuts ...

  4. Spring Boot(六)自定义事件及监听

    事件及监听并不是SpringBoot的新功能,Spring框架早已提供了完善的事件监听机制,在Spring框架中实现事件监听的流程如下: 自定义事件,继承org.springframework.con ...

  5. Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener

    前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...

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

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  7. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  8. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

  9. Vue.js 源码分析(十四) 基础篇 组件 自定义事件详解

    我们在开发组件时有时需要和父组件沟通,此时可以用自定义事件来实现 组件的事件分为自定义事件和原生事件,前者用于子组件给父组件发送消息的,后者用于在组件的根元素上直接监听一个原生事件,区别就是绑定原生事 ...

随机推荐

  1. Guava学习笔记之Maps(1):Maps.uniqueIndex(Iterable, Function)

    Guava官方文档 https://github.com/google/guava/wiki/CollectionUtilitiesExplained 官方文档这样描述: [`Maps.uniqueI ...

  2. java模式之一------代理模式

    代理模式的概念:对其他对象提供一种代理以控制对这个对象的访问 代理模式的三种实现 (1)静态代理 静态代理在使用时,需要定义接口或者父类,被代理对象与代理对象一起实现相同的接口或者是继承相同父类. e ...

  3. 面向对象,继承,浏览器,上传文件, ajax

    'use strict'; //父类 class Student2{ constructor(name){ this.name = name || 'tom'; } hello(){ console. ...

  4. mybatis大框架

    MyBatis   开源的数据持久化层框架 实体类与SQL语句之间建立映射关系 一:MyBatis前身是IBatis,本是Apache的一个开源的项目, 基于SQL语法,简单易学 ,是耦合度降低,方便 ...

  5. MAVLink Onboard Integration Tutorial

    MAVLink Onboard Integration Tutorial MAVLink is a header-only library, which means that you don't ha ...

  6. C#调用SQlite常见问题汇总

    最近在做SQLite开发,开发环境是VS2010+ SQLite Ado.Net data Provider.这套Data Provider程序是基于System.Data.SQLite 1.0.66 ...

  7. Fragment初探

    Fragment允许将Activity拆分成多个完全独立封装的可重用的组件,每个组件有它自己的生命周期和UI布局.Fragment最大的优点是为不同屏幕大小创建灵活的UI.每个Fragment都是独立 ...

  8. python 并发socketserver模块

    1.源码class 1.server类:处理链接 +------------+ | BaseServer | +------------+ | v +-----------+ +----------- ...

  9. Linux 虚拟机配置-network is unreachable

    配置虚拟机时,遇到network is unreachable,根据网上找来的方法处理,最终自己试过,成功修改的方法在这里记录一下: 修改虚拟机的网络适配器:桥接,复制物理机网络 vim /etc/s ...

  10. leetcode coding base

    1. Contains Duplicate 2. Contains Duplicate II 3. Contains Duplicate III