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); + } }