美文网首页
4.13.3应用程序事件

4.13.3应用程序事件

作者: 仙境源地 | 来源:发表于2019-10-16 17:47 被阅读0次

事件是继承ApplicationEvent的类,
任何bean都可以通过实现ApplicationListener<T>接口来监听事件;当配置时,ApplicationContext会自动注册实现此接口的任何bean作为监听器。
事件通过使用ApplicationEventPublisher.publishEvent()方法发布

package com.apress.prospring5.ch4;

import org.springframework.context.ApplicationEvent;

public class MessageEvent extends ApplicationEvent {
    private String msg;

    public MessageEvent(Object source, String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMessage() {
        return msg;
    }
}
package com.apress.prospring5.ch4;

import org.springframework.context.ApplicationListener;

public class MessageEventListener implements ApplicationListener<MessageEvent> {
    @Override
    public void onApplicationEvent(MessageEvent event) {
        MessageEvent msgEvt = event;
        System.out.println("Received: " + msgEvt.getMessage());
    }
}
<?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.xsd">

    <bean id="publisher" class="com.apress.prospring5.ch4.Publisher"/>

    <bean id="messageEventListener" 
        class="com.apress.prospring5.ch4.MessageEventListener"/>
</beans>
package com.apress.prospring5.ch4;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Publisher implements ApplicationContextAware {
    private ApplicationContext ctx;

    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.ctx = applicationContext;
    }

    public void publish(String message) {
        ctx.publishEvent(new MessageEvent(this, message));
    }

    public static void main(String... args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath:spring/app-context-xml.xml");

        Publisher pub = (Publisher) ctx.getBean("publisher");
        pub.publish("I send an SOS to the world... ");
        pub.publish("... I hope that someone gets my...");
        pub.publish("... Message in a bottle");
    }
}

执行结果

Received: I send an SOS to the world...
Received: ... I hope that someone gets my...
Received: ... Message in a bottle

相关文章

  • 4.13.3应用程序事件

    事件是继承ApplicationEvent的类,任何bean都可以通过实现ApplicationListener<...

  • 005.Spring Boot ApplicationEvent

    Spring应用程序事件 一、定义应用程序事件 MessageEvent.java 二、发布应用程序事件 三、订阅...

  • IOS事件的传递和响应

    事件描述 事件:用户和应用程序之间的交互IOS应用程序可以接收许多不同类型的事件吗,目前IOS事件分为4类: UI...

  • Java FX学习知识点(二)

    鼠标事件实例 介绍:在JavaFX中,我们可以开发GUI应用程序、Web应用程序和图形应用程序。在这样的应用程序中...

  • PyQt5编程(11):信号与事件的处理—指定信号处理程序

    所有的GUI应用程序都是基于事件驱动的。事件主要由应用程序的用户产生,但也可以由其他方式生成。比如,网络连接,定时...

  • 本地广播原理

    简介 本地广播:发送的广播事件不被其他应用程序获取,也不能响应其他应用程序发送的广播事件。本地广播只能被动态注册,...

  • Ionic 3 Events

    Events 是一个发布-订阅式事件系统,用于在应用程序中发送和响应应用程序级别的事件。允许您通过简单地发布包含一...

  • C#—事件的使用

    事件 Event 事件是委托的一个实例。 事件是一个操作,应用程序需要在事件发生时响应事件。例如,中断。事件是用于...

  • ios事件之UIEvent类详解(一)

    前言:我们知道,一个应用程序可以接收许多不同类型的事件,包括触摸事件、运动事件、远程控制事件和按压事件。触摸事件是...

  • ios 事件传递链

    1.app手机触摸事件 当点击屏幕,应用程序会添加事件到UIApplication事件队列,当触摸事件出队列后UI...

网友评论

      本文标题:4.13.3应用程序事件

      本文链接:https://www.haomeiwen.com/subject/jomumctx.html