mem*_*emn 5 java spring scope prototype event-listener
假设我有两个组件 X 和 Y,其中 X 是单例,而 Y 不是。
当我发布XUpdateEvent时,没有问题,我可以捕获该事件。但是,对于 YUpdateEvent 我无法捕获事件。Spring 为每个触发的事件创建新实例,而不是使用已经创建的实例。
那么,我是否需要编写自定义范围?或者EventListener有设置吗?
为了显示:
@Component
public class X{
@EventListener
public void onUpdate(XUpdateEvent event){
// fine.
}
}
@Component
@Scope("prototype")
public class Y{
@EventListener
public void onUpdate(YUpdateEvent event){
// calls new instance of Y for each event.
// Event should be fired for created instances.
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1649 次 |
最近记录: |