英文:
Spring application events without extending ApplicationEvent
问题
有没有办法在不扩展ApplicationEvent
的情况下定义自定义应用程序事件?我们基于端口和适配器原则拥有子模块,domain
模块没有任何外部依赖项,如 spring-context
。而 application
模块可以有 Spring 依赖以及 EventListener
和 EventPublisher
类。
事件类似乎是特定于域的,应该独立于任何框架定义。但是,根据当前 Spring 定义事件的方法,我们必须依赖于 spring-context
。有没有替代方案?
英文:
Is there any way to define custom application events without extending ApplicationEvent
?
We are having submodules based on ports and adapters principle, domain
module not having any any external dependencies like spring-context
. And application
module can have Spring dependency and EventListener
and EventPublisher
classes.
Event classes seems domain specific, and should be defined independent of any framework. But with current Spring's approach for defining events, we have to depend on spring-context
. Any alternatives?
答案1
得分: 2
你可以使用 [ApplicationEventPublisher
] 发布任何你喜欢的内容。这从 Spring 4.2 开始就已经可以做到了,它引入了 @EventListener
。从那时起,你就不需要再扩展 ApplicationEvent
了。
因此,你的知识已经过时。
英文:
You can publish whatever you like with the [ApplicationEventPublisher
]. This has been the case since Spring 4.2 which saw the introduction of @EventListener
. Since then you don't need to extend ApplicationEvent
anymore.
Hence your knowledge was/is outdated.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论