Hi,
I created this issue with the springboot team and they referred me here : https://github.com/spring-projects/spring-boot/issues/40858
I am using springboot 2.7.14 and running Java 17.
I have created this class:
class RequestContextHolder {
private var headers: Map<String, String?>? = null
fun setHeaders(headers: Map<String, String?>) {
this.headers = headers
}
fun getHeaders(): Map<String, String?>? {
return this.headers
}
}
Declared it as a request scoped bean here:
@Bean
@RequestScope
fun requestContextHolder(): RequestContextHolder {
return RequestContextHolder()
}
Used the bean as a dependency in my web filter as follows
@Component
class AppRequestContextFilter(
private val requestContextHolder: RequestContextHolder
) : OncePerRequestFilter() {
---
}
But as opposed to my expectation, the instance of RequestContextHolder
being returned seems to retain the values from the previous requests.
This issue goes away if I instead do the following and make the RequestContextHolder
class a Spring component.
@Component
@RequestScope
class RequestContextHolder {
private var headers: Map<String, String?>? = null
fun setHeaders(headers: Map<String, String?>) {
this.headers = headers
}
fun getHeaders(): Map<String, String?>? {
return this.headers
}
}
Is this inconsistency between @Component
and @Bean
expected? If so, I do not see it documented anywhere. Please clarify.
Comment From: snicoll
Did you try any of the suggestions before creating this issue?
Is this inconsistency between @Component and @Bean expected?
No. It should work whether you put it on @Bean
or the type itself. If you want support, please share a small sample we can run ourselves with instructions on how to reproduce. You can attach a zip to this issue or push the code to a separate GitHub repository.
Comment From: sbrannen
I'm guessing the difference might be that the presence of @Component
is causing the class to be open
due to the kotlin-spring
plugin (thanks to @sdeleuze for the link).
@mgunasekha-chwy, what happens if you make that non-annotated class open via open class RequestContextHolder
?
Comment From: mgunasekha-chwy
@sbrannen I tried declaring the RequestContextHolder
as open
to no success when using @Bean
. The issue still reproduces in this case
Comment From: mgunasekha-chwy
@snicoll There were a couple of suggestions in the other thread. I tried the suggestion about adding proxymode and that did not resolve the issue. Also I am using @RequestScope
which is an alias for adding a proxymode anyway, so not sure if that changes anything. : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/context/annotation/RequestScope.html
About the other suggestion to use an ObjectProvider
, that sounded like a workaround and I am happy to use @Component
in that case.
I just wanted to flag this in case it is a bug that need to be fixed within the framework.
Comment From: sbrannen
Thanks for trying out open
and providing feedback, @mgunasekha-chwy.
As @snicoll mentioned, if you'd like us to investigate this further...
please share a small sample we can run ourselves with instructions on how to reproduce. You can attach a zip to this issue or push the code to a separate GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: sbrannen
Is this inconsistency between
@Component
and@Bean
expected? If so, I do not see it documented anywhere.
It turns out this is to be expected, and it is documented in the Spring Projects in Kotlin section of the reference manual.
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 thekotlin-spring
plugin. A typical use case is@Scope
and its popular@RequestScope
specialization.
@mgunasekha-chwy, what happens if you make the class and its methods open
as follows?
open class RequestContextHolder {
private var headers: Map<String, String?>? = null
open fun setHeaders(headers: Map<String, String?>) {
this.headers = headers
}
open fun getHeaders(): Map<String, String?>? {
return this.headers
}
}
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.