Spring事件机制支持发布应用程序事件并监听这些事件。如以下问题所解释:Scoped Spring events possible? spring事件仅通知当前请求会话侦听器。所以我的问题是可以通知所有现有的作用域bean。
代码示例:
@Controller
public class FooController {
@Autowired
private ApplicationEventPublisher publisher;
@GetMapping("/fireEvent")
public void notifyAllScopedBeans() {
publisher.publishEvent(new FooEvent(this));
}
}
@SessionScope
@Component
public class FooListener {
private String username = "this bean username"
@EventListener(FooEvent.class)
public void listen() {
System.out.println("I'm listening. PS : I am
"+this.username);
}
}