Publishing Events from Aggregate Roots

由存储库管理的实体是聚合根。在领域驱动设计应用程序中,这些聚合根通常发布领域事件。Spring 数据提供了一个名为 @DomainEvents 的注释,你可以将它用于你的聚合根的方法,使发布尽可能容易,如下例所示:

Entities managed by repositories are aggregate roots. In a Domain-Driven Design application, these aggregate roots usually publish domain events. Spring Data provides an annotation called @DomainEvents that you can use on a method of your aggregate root to make that publication as easy as possible, as shown in the following example:

Exposing domain events from an aggregate root
class AnAggregateRoot {

    @DomainEvents 1
    Collection<Object> domainEvents() {
        // … return events you want to get published here
    }

    @AfterDomainEventPublication 2
    void callbackMethod() {
       // … potentially clean up domain events list
    }
}
1 The method that uses @DomainEvents can return either a single event instance or a collection of events. It must not take any arguments.
2 After all events have been published, we have a method annotated with @AfterDomainEventPublication. You can use it to potentially clean the list of events to be published (among other uses).

只要调用以下 Spring 数据存储库方法之一,就会调用这些方法:

The methods are called every time one of the following a Spring Data repository methods are called:

  • save(…), saveAll(…)

  • delete(…), deleteAll(…), deleteAllInBatch(…), deleteInBatch(…)

请注意,这些方法将聚合根实例作为参数。这就是为什么 deleteById(…) 明显缺失的原因,因为实现可能会选择发出一个查询来删除该实例,因此我们永远无法首先访问聚合实例。

Note, that these methods take the aggregate root instances as arguments. This is why deleteById(…) is notably absent, as the implementations might choose to issue a query deleting the instance and thus we would never have access to the aggregate instance in the first place.