Provide guidelines for using Kotlin properties with proxies (@RequestScope
and similar use cases)
#32287
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
theme: kotlin
An issue related to Kotlin support
type: documentation
A documentation task
Milestone
Problem statement
Hi team,
I noticed a discrepancy in behavior when using the
@RequestScope
annotation.Per the documentation I expect each request to produce a new bean with a fresh set of properties.
However, I observed two different outcomes, depending on the language of the model, Java vs Kotlin.
Example
Spring boot version: 3.2.2
I tested this by making repeated request to
http://localhost:8080/uniqueRequest
When using a Java model, I noticed the messageId is appropriately assigned when chosen by chance and appropriately cleared/set to empty when not chosen by chance on each subsequent request.
When using a Kotlin model, I noticed the messageId is appropriately assigned when chosen by chance and the property is unexpectedly carried over when not chosen by chance on each subsequent request.
RequestContextConfig.kt
HelloController.kt
Here are the two models, which show the different behaviors
Kotlin
Java
Observed output
Here are the results when making subsequent calls with each model (Kotlin and Java)
"Random hit" and "random not hit" indicates whether the random if statement in the controller was evaluated to true. (This was verified with a breakpoint)
Kotlin
Java
Why would the property be cached/saved when the model is defined with Kotlin?
The text was updated successfully, but these errors were encountered: