From ce7a2e8ea556e90764dfcc26ad63fbba7c71a370 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 23 Aug 2017 15:28:59 +0200 Subject: [PATCH] DATACMNS-1143 - Improve design of AbstractAggregateRoot. Methods for domain event registration and access are now not following bean semantics anymore to avoid those accidentally leaking into formats that use the bean spec (e.g. Jackson to produce JSON). --- .../data/domain/AbstractAggregateRoot.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java b/src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java index eb9748241..4164520b4 100644 --- a/src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java +++ b/src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java @@ -15,18 +15,19 @@ */ package org.springframework.data.domain; -import lombok.Getter; - import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import org.springframework.data.annotation.Transient; import org.springframework.util.Assert; /** * Convenience base class for aggregate roots that exposes a {@link #registerEvent(Object)} to capture domain events and - * expose them via {@link #getDomainEvents()}. The implementation is using the general event publication mechanism - * implied by {@link DomainEvents} and {@link AfterDomainEventPublication}. If in doubt or need to customize anything - * here, rather build your own base class and use the annotations directly. + * expose them via {@link #domainEvents())}. The implementation is using the general event publication mechanism implied + * by {@link DomainEvents} and {@link AfterDomainEventPublication}. If in doubt or need to customize anything here, + * rather build your own base class and use the annotations directly. * * @author Oliver Gierke * @author Christoph Strobl @@ -34,14 +35,10 @@ import org.springframework.util.Assert; */ public class AbstractAggregateRoot { - /** - * All domain events currently captured by the aggregate. - */ - @Getter(onMethod = @__(@DomainEvents)) // - private transient final List domainEvents = new ArrayList<>(); + private transient final @Transient List domainEvents = new ArrayList<>(); /** - * Registers the given event object for publication on a call to a Spring Data repository's save method. + * Registers the given event object for publication on a call to a Spring Data repository's save methods. * * @param event must not be {@literal null}. * @return @@ -59,7 +56,15 @@ public class AbstractAggregateRoot { * repositories. */ @AfterDomainEventPublication - public void clearDomainEvents() { + protected void clearDomainEvents() { this.domainEvents.clear(); } + + /** + * All domain events currently captured by the aggregate. + */ + @DomainEvents + protected Collection domainEvents() { + return Collections.unmodifiableList(domainEvents); + } }