/**
* Convenience base class for aggregate roots that exposes a {@link #registerEvent(Object)} to capture domain events and
* 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
* @since 1.13
*/
public class AbstractAggregateRoot<A extends AbstractAggregateRoot<A>> {
private transient final @Transient List<Object> domainEvents = new ArrayList<>();
/**
* 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 the event that has been added.
* @see #andEvent(Object)
*/
protected <T> T registerEvent(T event) {
Assert.notNull(event, "Domain event must not be null");
this.domainEvents.add(event);
return event;
}
/**
* Clears all domain events currently held. Usually invoked by the infrastructure in place in Spring Data
* repositories.
*/
@AfterDomainEventPublication
protected void clearDomainEvents() {
this.domainEvents.clear();
}
/**
* All domain events currently captured by the aggregate.
*/
@DomainEvents
protected Collection<Object> domainEvents() {
return Collections.unmodifiableList(domainEvents);
}
/**
* Adds all events contained in the given aggregate to the current one.
*
* @param aggregate must not be {@literal null}.
* @return the aggregate
*/
@SuppressWarnings("unchecked")
protected final A andEventsFrom(A aggregate) {
Assert.notNull(aggregate, "Aggregate must not be null");
this.domainEvents.addAll(aggregate.domainEvents());
return (A) this;
}
/**
* Adds the given event to the aggregate for later publication when calling a Spring Data repository's save-method.
* Does the same as {@link #registerEvent(Object)} but returns the aggregate instead of the event.
*
* @param event must not be {@literal null}.
* @return the aggregate
* @see #registerEvent(Object)
*/
@SuppressWarnings("unchecked")
protected final A andEvent(Object event) {
registerEvent(event);
return (A) this;
}
}
@Entity
public class Post extends AbstractAggregateRoot<Post> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
// ์ฌ๊ธฐ์ event๋ฅผ AbstractAggregateRoot๋ฅผ ํตํด์ ๋ฑ๋กํ๋ค.
public void registerEvent() {
PostPublishedEvent postPublishedEvent = new PostPublishedEvent(this);
this.registerEvent(postPublishedEvent);
}
}
@Override
@Transactional
public void run(ApplicationArguments args) throws Exception {
Post post = new Post();
post.setName("event publish test");
post.setDescription("event description");
post.registerEvent(); // <-- ๋๋ฉ์ธ ๋ด์์ ์ ์ธํ event ๋ฑ๋ก์ ํ๋ฉด
postRepository.save(post); // <-- save ์์ ์ publish ๋๋ค.
}
public class DomainClassConverter<T extends ConversionService & ConverterRegistry>
implements ConditionalGenericConverter, ApplicationContextAware
@GetMapping("/posts/{id}")
public String getAPost(@PathVariable Long id) {
Optional<Post> byId = postRepository.findById(id);
Post post = byId.get();
return post.getTitle();
}
@GetMapping("/posts/{id}")
public String getAPost(@PathVariable(โidโ) Post post) {
return post.getTitle();
}
@GetMapping("/posts")
public void testHandler(Pageable pageable){
}