Spring源码解读:EventListener接口

本文探讨了如何在Spring框架中使用EventListener接口实现特定事件的监听。详细讲解了如何创建并发布自定义的ApplicationEvent,同时介绍了同步和异步操作,特别是通过ApplicationEventMulticaster配合TaskExecutor实现异步事件处理的机制。
摘要由CSDN通过智能技术生成

在Spring中有这样一种情景就是,我只想监听我想要的那个事件其他的我不需要。OK,在Spring中确实可以实现这种需求,先看看怎样监听的

package com.yohanliyanage.blog.springevents;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MyEventListener implements ApplicationListener {
    private static final Log LOG = LogFactory.getLog(MyEventListener.class);

    public void onApplicationEvent(ApplicationEvent event) {
        LOG.info("Event Occurred : " + event);
    }
}

< ?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Adding the listener class to Spring Container automatically registers it for events -->
<bean class="com.yohanliyanage.blog.springevents.MyEventListener" />
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值