This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data Commons 3.5.4!

Ahead of Time Optimizations

This chapter covers Spring Data’s Ahead of Time (AOT) optimizations that build upon Spring’s Ahead of Time Optimizations.

Best Practices

Annotate your Domain Types

During application startup, Spring scans the classpath for domain classes for early processing of entities. By annotating your domain types with Spring Data Store specific @Table, @Document or @Entity annotations you can aid initial entity scanning and ensure that those types are registered with ManagedTypes for Runtime Hints. Classpath scanning is not possible in native image arrangements and so Spring has to use ManagedTypes for the initial entity set.

Ahead of Time Code Generation

Ahead of time code generation is not limited to usage with GraalVM Native Image but also offers benefits when working with regular deployments and can help optimize startup performance on the jvm.

If Ahead of Time compilation is enabled Spring Data can (depending on the actual Module in use) contribute several components during the AOT phase of your build.

  • Bytecode for generated Type/Property Accessors

  • Sourcecode for the defined Repository Interfaces

  • Repository Metadata in JSON format

Each of the above is enabled by default. However, there users may fine tune the configuration with following options.

spring.aot.data.accessors.enabled

Boolean flag to control contribution of Bytecode for generated Type/Property Accessors

spring.aot.data.accessors.include

Comma separated list of FQCN for which to contribute Bytecode for generated Type/Property Accessors. Ant-style include patterns matching package names (e.g. com.acme.**) or type names inclusion. Inclusion pattern matches are evaluated before exclusions for broad exclusion and selective inclusion.

spring.aot.data.accessors.exclude

Comma separated list of FQCN for which to skip contribution of Bytecode for generated Type/Property Accessors. Ant-style exclude patterns matching package names (e.g. com.acme.**) or type names exclusion. Exclusion pattern matches are evaluated after inclusions for broad exclusion and selective inclusion.

spring.aot.repositories.enabled

Boolean flag to control contribution of Source Code for Repository Interfaces

spring.aot.[module-name].repositories.enabled

Boolean flag to control contribution of Source Code for Repository Interfaces for a certain module (eg. jdbc, jpa, mongodb, cassandra)

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, annotated, and named queries that are known at build-time. This optimization moves query method processing from runtime to build-time, which can lead to a significant 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_AotRepository and is placed in the same package as the repository interface.

Native Image Runtime Hints

Running an application as a native image requires additional information compared to a regular JVM runtime. Spring Data contributes 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)