You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/repositories.adoc
+32-2Lines changed: 32 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -218,7 +218,7 @@ NOTE: Note, that the intermediate repository interface is annotated with `@NoRep
218
218
Using a unique Spring Data module in your application makes things simple hence, all repository interfaces in the defined scope are bound to the Spring Data module. Sometimes applications require using more than one Spring Data module. In such case, it's required for a repository definition to distinguish between persistence technologies. Spring Data enters strict repository configuration mode because it detects multiple repository factories on the class path. Strict configuration requires details on the repository or the domain class to decide about Spring Data module binding for a repository definition:
219
219
220
220
1. If the repository definition <<repositories.multiple-modules.types,extends the module-specific repository>>, then it's a valid candidate for the particular Spring Data module.
221
-
2. If the domain class is <<repositories.multiple-modules.annotations,annotated with the module-specific type annotation>>, then it's a valid candidate for the particular Spring Data module. Spring Data modules accept either 3rd party annotations (such as JPA's `@Entity`) or provide own annotations such as `@Document` for Spring Data MongoDB/Spring Data Elasticsearch.
221
+
2. If the domain class is <<repositories.multiple-modules.annotations,annotated with the module-specific type annotation>>, then it's a valid candidate for the particular Spring Data module. Spring Data modules accept either 3rd party annotations (such as JPA's `@Entity`) or provide own annotations such as `@Document` for Spring Data MongoDB/Spring Data Elasticsearch.
222
222
223
223
[[repositories.multiple-modules.types]]
224
224
.Repository definitions using Module-specific Interfaces
@@ -306,7 +306,7 @@ public class Person {
306
306
This example shows a domain class using both JPA and Spring Data MongoDB annotations. It defines two repositories, `JpaPersonRepository` and `MongoDBPersonRepository`. One is intended for JPA and the other for MongoDB usage. Spring Data is no longer able to tell the repositories apart which leads to undefined behavior.
307
307
====
308
308
309
-
<<repositories.multiple-modules.types,Repository type details>> and <<repositories.multiple-modules.annotations,identifying domain class annotations>> are used for strict repository configuration identify repository candidates for a particular Spring Data module. Using multiple persistence technology-specific annotations on the same domain type is possible to reuse domain types across multiple persistence technologies, but then Spring Data is no longer able to determine a unique module to bind the repository.
309
+
<<repositories.multiple-modules.types,Repository type details>> and <<repositories.multiple-modules.annotations,identifying domain class annotations>> are used for strict repository configuration identify repository candidates for a particular Spring Data module. Using multiple persistence technology-specific annotations on the same domain type is possible to reuse domain types across multiple persistence technologies, but then Spring Data is no longer able to determine a unique module to bind the repository.
310
310
311
311
The last way to distinguish repositories is scoping repository base packages. Base packages define the starting points for scanning for repository interface definitions which implies to have repository definitions located in the appropriate packages. By default, annotation-driven configuration uses the package of the configuration class. The <<repositories.create-instances.spring,base package in XML-based configuration>> is mandatory.
312
312
@@ -736,6 +736,36 @@ A corresponding attribute is available in the XML namespace.
736
736
----
737
737
====
738
738
739
+
[[core.domain-events]]
740
+
== Publishing events from aggregate roots
741
+
742
+
Entities managed by repositories are aggregate roots.
743
+
In a Domain-Driven Design application, these aggregate roots usually publish domain events.
744
+
Spring Data provides an annotation `@DomainEvents` you can use on a method of your aggregate root to make that publication as easy as possible.
745
+
746
+
.Exposing domain events from an aggregate root
747
+
====
748
+
[source, java]
749
+
----
750
+
class AnAggreagteRoot {
751
+
752
+
@DomainEvents <1>
753
+
Collection<Object> domainEvents() {
754
+
// … return events you want to get published here
755
+
}
756
+
757
+
@AfterDomainEventsPublication <2>
758
+
void callbackMethod() {
759
+
// … potentially clean up domain events list
760
+
}
761
+
}
762
+
----
763
+
<1> The method using `@DomainEvents` can either return a single event instance or a collection of events. It must not take any arguments.
764
+
<2> After all events have been published, a method annotated with `@AfterDomainEventsPublication`. It e.g. can be used to potentially clean the list of events to be published.
765
+
====
766
+
767
+
The methods will be called every time one of a Spring Data repository's `save(…)` methods is called.
0 commit comments