-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Document Kotlin internal
modifier impact on @Bean
#31985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is likely a side effect of Kotlin name mangling of internal functions. If you decompile the JVM bytecode, you will see: @Bean({"beanA"})
@NotNull
public Companion.InternalInterface1 explicitNameInternalBeanA$demo_kotlin_internal_test() {
return (Companion.InternalInterface1)(new Companion.InternalClass1());
} You use case should work when using mangled name like |
Thank you, @sdeleuze . |
After a deeper look, it does not seem obvious how we can support resolving Kotlin internal function names at It would be doable to identify Kotlin classes using
As a consequence, I am turning this issue into a documentation one. |
internal
function impact on @Bean
internal
function impact on @Bean
internal
modifier impact on @Bean
@sdeleuze, Thank you for looking into this and adding documentation for it. Regarding:
It might be good to also suggest in the docs the alternative of adding an explicit bean name, i.e.
In fact, I'd suggest making that the primary recommendation over using the mangled name when injecting. I don't see the specific convention of appending the module name for mangling internal functions documented anywhere in the Kotlin docs, so perhaps that could change. Moreover, if one renames a module, that could break Spring configuration in a potentially unanticipated way. |
Affects: <Spring Framework version 5.3.31>
Spring discriminates public beans by implicit bean name and injected parameter name. If multiple public beans match an unqualified injection parameter, Spring matches the parameter name to the implicit bean name that it derives from the
@Bean
function name.However, we find different behavior when the beans involved are Kotlin
internal
. Spring fails to use implicit bean names and injection parameter names to discriminate between multiple Kotlininternal
beans that match an injection parameter type. Instead,NoUniqueBeanDefinitionException
is thrown.Our project has many Kotlin
@Bean
functions declaredinternal
to restrict their visibility to the declaring module. Many of those beans are ofinternal
types because we want visibility of those types restricted to the declaring module. When a bean's type isinternal
, the@Bean
function must also beinternal
.Our developers found that, to make Spring discriminate between multiple such
internal
beans of the same type, we must provide both explicit@Bean
name Strings and injection parameter qualifier Strings (e.g.@Named("someBeanName")
).We find it counterintuitive that Kotlin
internal
bean implicit names and injection behave differently than public beans. Preferably, they should behave the same. Else, document the different behavior. Apologies if such documentation exists; we have not found it.Minimal reproducible example:
SpringInternalBeanFunctionBugReproducerTest.kt
:build.gradle
:The text was updated successfully, but these errors were encountered: