Spring框架事件 [英] Spring Framework Events

查看:155
本文介绍了Spring框架事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过Spring框架的文档阅读,发现一节使用的ApplicationContext春季引发事件。看完几段后,我发现,春天的事件被同步提高。有什么办法来提高异步事件?你的帮助是非常AP preciated。我期待类似的东西,这将有助于我完成我的模块。

I was reading through spring framework documentation and found a section on raising events in Spring using ApplicationContext. After reading few paragraphs I found that spring events are raised synchronously. Is there any way to raise asynchronous events? your help is much appreciated. I am looking something similar which would help me to complete my module.

推荐答案

最简单的异步ApplicationListener:

Simplest asynchronous ApplicationListener:

出版商:

@Autowired
private SimpleApplicationEventMulticaster simpleApplicationEventMulticaster;

@Autowired
private AsyncTaskExecutor asyncTaskExecutor;

// ...

simpleApplicationEventMulticaster.setTaskExecutor(asyncTaskExecutor);

// ...

ApplicationEvent event = new ApplicationEvent("");
simpleApplicationEventMulticaster.multicastEvent(event);

监听器:

@Component
static class MyListener implements ApplicationListener<ApplicationEvent> 
    public void onApplicationEvent(ApplicationEvent event) {
         // do stuff, typically check event subclass (instanceof) to know which action to perform
    }
}

您应该继承了ApplicationEvent与特定的事件。您可以在XML文件中配置SimpleApplicationEventMulticaster及其taksExecutor。

You should subclass ApplicationEvent with your specific events. You can configure SimpleApplicationEventMulticaster and its taksExecutor in an xml file.

您可能要实施ApplicationEventPublisherAware在监听器类和事件构造传递源对象(而不是空字符串)。

You might want to implement ApplicationEventPublisherAware in your listener class and pass a source object (instead of empty string) in the event constructor.

这篇关于Spring框架事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆