Spring注解/记录Spring自定义事件的方法

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

Spring annotations / ways to document spring custom events

问题

我正在开发一个庞大的Spring Boot应用,将其分成了不同的服务。

随着应用变得越来越大,我开始注意到一些服务之间存在深层耦合的情况,因此我在应用的某些部分使用了基本的发布/订阅模型,使用了Spring Events(ApplicationEvent)来降低耦合。

每个服务都会创建并发布自定义事件,其他服务订阅他们想要消费的事件。

我希望记录每个服务发布和订阅的事件,以帮助提高代码的可读性和调试效率。

在这个任务中,是否有特定的Spring注解或方法可以帮助实现?

英文:

I am working on a big spring boot application divided into different services.

As the application got larger, I started to see some deep coupling between services so I implemented a basic Pub/Sub model using Spring Events(ApplicationEvent) on some parts of the application to decrease coupling.

Each service creates and publishes custom events, other services subscribe to the events they want to consume.

I want to document what events each service publishes and subscribes to, in order to help with readability and debugging.

Is there any specific spring annotations/ways to help with this task?

答案1

得分: 1

通常我们只使用javadoc来进行这类型的文档编写。您可以在您服务的方法中这样做:

/**
 * @param idFilial 要获取的分店的ID
 * @return 如果搜索有返回,则返回分店对象,否则返回 Optional.empty()
 * @implNote 从ID查找特定分店
 */
Optional<Filial> findById(Long idFilial);
英文:

Usually we use only the javadoc to do this type of documentation. You can do like this in your service's method:

/**
 * @param idFilial to be fetched
 * @return Filial object in case there is a return in the search, otherwise an Optional.empty ()
 * @implNote Search for a specific Filial from an id
 * */
Optional &lt;Filial&gt; findById(Long idFilial);

答案2

得分: 1

我不知道具体的注解,但我通常的做法是使用sealed class来保存我的事件,以确保所有事件都存放在同一个地方:

sealed class MyEvent(source: Any, name: String) : ApplicationEvent(source) {

    data class SomeEvent(val source: Any, val name: String) : MyEvent(source, name)
    
    // ...
}

然后你可以在监听器上使用注解:

@Component
class SomeListener {

  @EventListener(condition = &quot;#event.name eq &#39;hey&#39;&quot;)
  fun handleConditionalListener(event: SomeEvent) {
    // 处理事件
  }
}

这有助于提高可发现性,因为你只需要寻找@EventListener即可。

你还可以有一个事件监听器来监听所有事件,然后可以根据你收集到的信息绘制出图表。

英文:

I don't know about specific annotations but what I usually do is to have a sealed class to hold my events so that I can be sure that all events reside in one place:

sealed class MyEvent(source: Any, name: String) : ApplicationEvent(source) {

    data class SomeEvent(val source: Any, val name: String) : MyEvent(source, name)
    
    // ...
}

Then you can use annotations on listeners:

@Component
class SomeListener {

  @EventListener(condition = &quot;#event.name eq &#39;hey&#39;&quot;)
  fun handleConditionalListener(event: SomeEvent) {
    // handle event
  }
}

This helps with disoverability since you only need to look for @EventListeners.

You can also have an event listener that listens to all events and then you can draw a diagram from the information you gathered there.

huangapple
  • 本文由 发表于 2020年10月13日 20:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64335080.html
匿名

发表评论

匿名网友

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

确定