From 0de3b3002919560bc19fede043049260a0c38efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 28 Feb 2024 18:33:39 +0100 Subject: [PATCH] Provide guidelines for using Kotlin properties with proxies A typical use case is `@Scope` and its popular `@RequestScope` specialization. Closes gh-32287 --- .../ROOT/pages/languages/kotlin/spring-projects-in.adoc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc b/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc index 9adb9517c11..6efa48274a4 100644 --- a/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc +++ b/framework-docs/modules/ROOT/pages/languages/kotlin/spring-projects-in.adoc @@ -9,7 +9,7 @@ in Kotlin. [[final-by-default]] == Final by Default -By default, https://discuss.kotlinlang.org/t/classes-final-by-default/166[all classes in Kotlin are `final`]. +By default, https://discuss.kotlinlang.org/t/classes-final-by-default/166[all classes and member functions in Kotlin are `final`]. The `open` modifier on a class is the opposite of Java's `final`: It allows others to inherit from this class. This also applies to member functions, in that they need to be marked as `open` to be overridden. @@ -38,6 +38,12 @@ Meta-annotation support means that types annotated with `@Configuration`, `@Cont `@RestController`, `@Service`, or `@Repository` are automatically opened since these annotations are meta-annotated with `@Component`. +WARNING: Some use cases involving proxies and automatic generation of final methods by the Kotlin compiler require extra +care. For example, a Kotlin class with properties will generate related `final` getters and setters. In order +to be able to proxy related methods, a type level `@Component` annotation should be preferred to method level `@Bean` in +order to have those methods opened by the `kotlin-spring` plugin. A typical use case is `@Scope` and its popular +`@RequestScope` specialization. + https://start.spring.io/#!language=kotlin&type=gradle-project[start.spring.io] enables the `kotlin-spring` plugin by default. So, in practice, you can write your Kotlin beans without any additional `open` keyword, as in Java.