Detect ReadPreference for annotated aggregations and inherited find methods.

This commit reduces the API surface of the ReadPreference annotation leaving fine grained control such as Tags to the Template API.
Next to supporting read preference for annotated queries we now also make sure to apply read preferences for annotated aggregation methods as well as predefined finder methods running queries.

See: #2971
Original Pull Request: #4503
This commit is contained in:
Christoph Strobl
2023-09-27 14:55:06 +02:00
parent 16b97a26cc
commit 715231e9ef
23 changed files with 415 additions and 166 deletions

View File

@@ -827,21 +827,17 @@ interface GameRepository extends Repository<Game, String> {
== Read Preferences
The `@ReadPreference` annotation allows you to configure MongoDB's ReadPreferences
The `@ReadPreference` annotation allows you to configure MongoDB's ReadPreferences.
.Example of read preferences
====
[source,java]
----
@ReadPreference(
value = "primaryPreferred",
tags = {@ReadPreferenceTag(name = "local", value = "east"), @ReadPreferenceTag(name = "pre", value = "west")},
maxStalenessSeconds = 150
) <1>
@ReadPreference("primaryPreferred") <1>
public interface PersonRepository extends CrudRepository<Person, String> {
@ReadPreference(value = "secondaryPreferred") <2>
@ReadPreference("secondaryPreferred") <2>
List<Person> findWithReadPreferenceAnnotationByLastname(String lastname);
@Query(readPreference = "nearest") <3>
@@ -849,8 +845,13 @@ public interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findWithReadPreferenceAtTagByFirstname(String firstname); <4>
----
<1> Configure read preference for all repository operations that do not have a query-level definition. Therefore, in this case the read preference mode will be `primaryPreferred`
<1> Configure read preference for all repository operations (including inherited, non custom implementation ones) that do not have a query-level definition. Therefore, in this case the read preference mode will be `primaryPreferred`
<2> Use the read preference mode defined in annotation `ReadPreference`, in this case secondaryPreferred
<3> The `@Query` annotation defines the `read preference mode` alias which is equivalent to adding the `@ReadPreference` annotation.
<4> This query will use the read preference mode defined in the repository.
====
[TIP]
====
The `MongoOperations` and `Query` API offer more fine grained control for `ReadPreference`.
====