有范围的春季活动可能吗

有范围的春季活动可能吗

本文介绍了有范围的春季活动可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring事件机制支持发布应用程序事件,并通过@EventListener批注在Spring组件中侦听这些事件.但是,在文档.我对Vaadin的具体需求是:

The Spring event mechanism supports publishing application events and listening to these events within Spring components via the @EventListener annotation. However, I cannot find anything about sending events within a specific scope in the documentation. My specific need for Vaadin is:

  • 在用户交互的情况下,发送事件(例如,已登录事件)
  • 此事件仅应由同一@UIScope中的bean消耗,即,不应影响其他用户UI
  • in context of a user interaction, send an event (e.g. logged-in event)
  • this event should only be consumed by beans in same @UIScope, i.e. other user UIs shouldn't be effected

有可能吗?注意:这并不是Vaadin真正的特有功能.我也可以问一下如何使用Spring Web MVC请求范围.

Is that possible? NOTE: This is not really specific to Vaadin. I could also ask how it would be done with Spring web mvc request scope.

推荐答案

TL; DR:我认为它已经可以按照您的要求工作

长版:

文档尚不清楚,但是尝试了一个简单的测试:

The documentation is somewhat unclear about this, but having tried a simple test :

发布事件的控制器:

@Controller
public class FooController {
    @Autowired
    private ApplicationEventPublisher publisher;

    @GetMapping("/fireEvent")
    public void fireEvent() {
        publisher.publishEvent(new FooEvent(this));
    }
}

还有一个侦听的作用域Bean:

And a scoped bean that listens :

@Scope(value = WebApplicationContext.SCOPE_REQUEST)
@Component
public class FooListener {
    @EventListener(FooEvent.class)
    public void listen() {
         System.out.println("I'm listening. PS : I am "+this.toString());
    }
}

并且在运行两个并发请求时,只有作用域相同的httprequest的侦听器才能获取事件.

And when running two concurrent requests, only the listener scoped to the the same httprequest gets the event.

我的解释(由于未深入研究,请带上一粒盐):

My interpretation (without having looked at it really deeply, so take it with a grain of salt) :

在我看来,ApplicationEventMulticasterListenerRetriever使用BeanFactory来获取先前已注册为侦听器(按其名称)的bean.显然,工厂将在当前范围内返回一个bean.

It seems to me that the ApplicationEventMulticaster's ListenerRetriever uses the BeanFactory to get the beans that were previously registered as listeners (by their names). And obviously, the factory will return a bean in the current scope.

这篇关于有范围的春季活动可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:10