DATACMNS-928 - Support for publishing events from aggregate roots.

Introduced @DomainEvents usable on a method of an aggregate root. The method will be invoked when the aggregate is passed to a repository to save and its returned value or values will be exposed via an ApplicationEventListener.

Also, the aggregate can expose a method annotated with @AfterDomainEventPublication which will be invoked once all events have been published.
This commit is contained in:
Oliver Gierke
2016-10-21 10:24:50 +02:00
parent a8b2eb1589
commit 802eb7deef
6 changed files with 570 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.domain;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
/**
* @author Oliver Gierke
*/
public class AbstractAggregateRoot {
private transient final @Getter(onMethod = @__(@DomainEvents)) List<Object> domainEvents = new ArrayList<Object>();
protected <T> T registerEvent(T event) {
this.domainEvents.add(event);
return event;
}
@AfterDomainEventPublication
public void clearDomainEvents() {
this.domainEvents.clear();
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.domain;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation to be used on a method of a Spring Data managed aggregate to get invoked after the events of an aggregate
* have been published.
*
* @author Oliver Gierke
* @see DomainEvents
* @soundtrack Benny Greb - September (Moving Parts Live)
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
public @interface AfterDomainEventPublication {
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.domain;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.ApplicationEventPublisher;
/**
* {@link DomainEvents} can be used on methods of aggregate roots managed by Spring Data repositories to publish the
* events returned by that method as Spring application events.
*
* @author Oliver Gierke
* @see ApplicationEventPublisher
* @see AfterDomainEventPublication
* @soundtrack Benny Greb - Soulfood (Moving Parts Live)
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
public @interface DomainEvents {
}