Spring应用事件,无需扩展ApplicationEvent

huangapple go评论75阅读模式
英文:

Spring application events without extending ApplicationEvent

问题

有没有办法在不扩展ApplicationEvent的情况下定义自定义应用程序事件?我们基于端口和适配器原则拥有子模块,domain 模块没有任何外部依赖项,如 spring-context。而 application 模块可以有 Spring 依赖以及 EventListenerEventPublisher 类。

事件类似乎是特定于域的,应该独立于任何框架定义。但是,根据当前 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.

huangapple
  • 本文由 发表于 2023年7月3日 11:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定