@@ -97,6 +97,9 @@ does not require the `kotlin-noarg` plugin if the module uses Spring Data object
97
97
[[injecting-dependencies]]
98
98
== Injecting Dependencies
99
99
100
+ [[favor-constructor-injection]]
101
+ === Favor constructor injection
102
+
100
103
Our recommendation is to try to favor constructor injection with `val` read-only (and
101
104
non-nullable when possible) {kotlin-docs}/properties.html[properties],
102
105
as the following example shows:
@@ -130,7 +133,41 @@ as the following example shows:
130
133
}
131
134
----
132
135
136
+ [[internal-functions-name-mangling]]
137
+ === Internal functions name mangling
138
+
139
+ Kotlin functions with the `internal` {kotlin-docs}/visibility-modifiers.html#class-members[visibility modifier] have
140
+ their names mangled when compiled to JVM bytecode, which has a side effect when injecting dependencies by name.
141
+
142
+ For example, this Kotlin class:
143
+ [source,kotlin,indent=0]
144
+ ----
145
+ @Configuration
146
+ class SampleConfiguration {
147
+
148
+ @Bean
149
+ internal fun sampleBean() = SampleBean()
150
+ }
151
+ ----
152
+
153
+ Translates to this Java representation of the compiled JVM bytecode:
154
+ [source,java,indent=0]
155
+ ----
156
+ @Configuration
157
+ @Metadata(/* ... */)
158
+ public class SampleConfiguration {
159
+
160
+ @Bean
161
+ @NotNull
162
+ public SampleBean sampleBean$demo_kotlin_internal_test() {
163
+ return new SampleBean();
164
+ }
165
+ }
166
+ ----
133
167
168
+ As a consequence, the related bean name represented as a Kotlin string is `"sampleBean\$demo_kotlin_internal_test"`,
169
+ instead of `"sampleBean"` for the regular `public` function use-case. Make sure to use the mangled name when injecting
170
+ such bean by name.
134
171
135
172
[[injecting-configuration-properties]]
136
173
== Injecting Configuration Properties
0 commit comments