Extensions

Kotlin extensions provide the ability to extend existing classes with additional functionality. Spring Data Kotlin APIs use these extensions to add new Kotlin-specific conveniences to existing Spring APIs.spring-doc.cn

Keep in mind that Kotlin extensions need to be imported to be used. Similar to static imports, an IDE should automatically suggest the import in most cases.spring-doc.cn

For example, Kotlin reified type parameters provide a workaround for JVM generics type erasure, and Spring Data provides some extensions to take advantage of this feature. This allows for a better Kotlin API.spring-doc.cn

To retrieve a list of SWCharacter objects in Java, you would normally write the following:spring-doc.cn

Flux<SWCharacter> characters = template.query(SWCharacter.class).inTable("star-wars").all()

With Kotlin and the Spring Data extensions, you can instead write the following:spring-doc.cn

val characters = template.query<SWCharacter>().inTable("star-wars").all()
// or (both are equivalent)
val characters : Flux<SWCharacter> = template.query().inTable("star-wars").all()

As in Java, characters in Kotlin is strongly typed, but Kotlin’s clever type inference allows for shorter syntax.spring-doc.cn

Spring Data for Apache Cassandra provides the following extensions:spring-doc.cn

  • Reified generics support for CassandraOperations (including async and reactive variants), CqlOperations (including async and reactive variants)FluentCassandraOperations, ReactiveFluentCassandraOperations, Criteria, and Query.spring-doc.cn

  • [kotlin.coroutines] extensions for ReactiveFluentCassandraOperations.spring-doc.cn