Skip to content

Commit 2596e29

Browse files
committed
Refine "Add AOT/Native support"
This commit review the support for AOT by only ignoring beans that are using an instance supplier. The Kotlin DSL has a way to register a bean by type where all inferences should happen as usual and that was previously ignored. This commit no longer ignores those beans so AOT can optimize them, and makes sure that they are not registered again when running with AOT optimizations. This change makes it so that the order in which beans are registered is now different when running with AOT optimizations, and we'll have to find a solution for that. See gh-29555
1 parent b888f36 commit 2596e29

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Diff for: spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.support
1818

19+
import org.springframework.aot.AotDetector
1920
import org.springframework.beans.factory.ObjectProvider
2021
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
2122
import org.springframework.beans.factory.config.BeanDefinition
@@ -181,6 +182,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
181182
role: Role? = null,
182183
order: Int? = null) {
183184

185+
// This version registers a regular bean definition that has been processed by AOT.
186+
if (AotDetector.useGeneratedArtifacts()) {
187+
return
188+
}
184189
val customizer = BeanDefinitionCustomizer { bd ->
185190
scope?.let { bd.scope = scope.name.lowercase() }
186191
isLazyInit?.let { bd.isLazyInit = isLazyInit }
@@ -191,7 +196,6 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
191196
description?.let { bd.description = description }
192197
role?.let { bd.role = role.ordinal }
193198
order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) }
194-
bd.setAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true)
195199
}
196200

197201
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context)

Diff for: spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ class BeanDefinitionDslTests {
220220
}
221221

222222
@Test
223-
fun `Declare beans with the functional Kotlin DSL flag them as to be ignored by AOT`() {
223+
fun `Declare beans flag them as to be ignored by AOT if needed`() {
224224
val beans = beans {
225-
bean<Foo>("one")
225+
bean("one") { foo() }
226226
bean<Bar>("two")
227227
}
228228

@@ -234,7 +234,7 @@ class BeanDefinitionDslTests {
234234
assertThat(context.getBeanDefinition("one").getAttribute(
235235
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
236236
assertThat(context.getBeanDefinition("two").getAttribute(
237-
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
237+
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isNull()
238238
}
239239

240240
}

0 commit comments

Comments
 (0)