Extend AOT Repository Support.

- Introduce AOT fragment base class.
- Refactor Delete execution to be reusable.
- Add support for updates.
- Add support for aggregations.
- Move types to repository package.
- Update documentation.

See: #4939
This commit is contained in:
Christoph Strobl
2025-04-07 11:26:36 +02:00
committed by Mark Paluch
parent 2aac1a59be
commit 6c6438ec21
38 changed files with 2837 additions and 1385 deletions

View File

@@ -53,6 +53,7 @@
** xref:mongodb/repositories/cdi-integration.adoc[]
** xref:repositories/query-keywords-reference.adoc[]
** xref:repositories/query-return-types-reference.adoc[]
** xref:mongodb/aot.adoc[]
// Observability
* xref:observability/observability.adoc[]

View File

@@ -0,0 +1,88 @@
= Ahead of Time Optimizations
This chapter covers Spring Data's Ahead of Time (AOT) optimizations that build upon {spring-framework-docs}/core/aot.html[Spring's Ahead of Time Optimizations].
[[aot.hints]]
== Runtime Hints
Running an application as a native image requires additional information compared to a regular JVM runtime.
Spring Data contributes {spring-framework-docs}/core/aot.html#aot.hints[Runtime Hints] during AOT processing for native image usage.
These are in particular hints for:
* Auditing
* `ManagedTypes` to capture the outcome of class-path scans
* Repositories
** Reflection hints for entities, return types, and Spring Data annotations
** Repository fragments
** Querydsl `Q` classes
** Kotlin Coroutine support
* Web support (Jackson Hints for `PagedModel`)
[[aot.repositories]]
== Ahead of Time Repositories
AOT Repositories are an extension to AOT processing by pre-generating eligible query method implementations.
Query methods are opaque to developers regarding their underlying queries being executed in a query method call.
AOT repositories contribute query method implementations based on derived or annotated queries, updates or aggregations that are known at build-time.
This optimization moves query method processing from runtime to build-time, which can lead to a significant bootstrap performance improvement as query methods do not need to be analyzed reflectively upon each application start.
The resulting AOT repository fragment follows the naming scheme of `<Repository FQCN>Impl__Aot` and is placed in the same package as the repository interface.
You can find all queries in their MQL form for generated repository query methods.
[TIP]
====
`spring.aot.repositories.enabled` property needs to be set to `true` for repository fragment code generation.
====
[NOTE]
====
Consider AOT repository classes an internal optimization.
Do not use them directly in your code as generation and implementation details may change in future releases.
====
=== Running with AOT Repositories
AOT is a mandatory step to transform a Spring application to a native executable, so it is automatically enabled when running in this mode.
It is also possible to use those optimizations on the JVM by setting the `spring.aot.enabled` and `spring.aot.repositories.enabled` System properties to `true`.
AOT repositories contribute configuration changes to the actual repository bean registration to register the generated repository fragment.
[NOTE]
====
When AOT optimizations are included, some decisions that have been taken at build-time are hard-coded in the application setup.
For instance, profiles that have been enabled at build-time are automatically enabled at runtime as well.
Also, the Spring Data module implementing a repository is fixed.
Changing the implementation requires AOT re-processing.
====
=== Eligible Methods in Data MongoDB
AOT repositories filter methods that are eligible for AOT processing.
These are typically all query methods that are not backed by an xref:repositories/custom-implementations.adoc[implementation fragment].
**Supported Features**
* Derived `find`, `count`, `exists` and `delete` methods
* Query methods annotated with `@Query` (excluding those containing SpEL)
* Methods annotated with `@Aggregation`
* Methods using `@Update`
* `@Hint` & `@ReadPreference` support
* `Page`, `Slice`, and `Optional` return types
* DTO Projections
**Limitations**
* `@Meta` annotations are not evaluated.
* Queries / Aggregations / Updates containing `SpEL` cannot be generated.
* Limited `Collation` detection.
* Reserved parameter names (must not be used in method signature) `finder`, `filterQuery`, `countQuery`, `deleteQuery`, `remover` `updateDefinition`, `aggregation`, `aggregationPipeline`, `aggregationUpdate`, `aggregationOptions`, `updater`, `results`, `fields`.
**Excluded methods**
* `CrudRepository` and other base interface methods
* Querydsl and Query by Example methods
* Methods whose implementation would be overly complex
* Query Methods obtaining MQL from a file
** Methods accepting `ScrollPosition` (e.g. `Keyset` pagination)
** Dynamic projections
** Geospatial Queries