Skip to content

Commit b8f9913

Browse files
committed
Add AOT/Native support
This commit adds AOT/Native support for beans that are contributed by the Kotlin DSL. Since they use an instance supplier, such beans are now configured to be ignored by AOT generation. They are part of the bean factory still so any hint generation works. This commit removes a previous attempt at fixing this issue when we were not checking for instance suppliers. Rather than skipping the initializr at runtime, it runs again as intended since their state can't be stored in AOT-generated code. Closes gh-29555
1 parent e741d6e commit b8f9913

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.context.support
1818

19-
import org.springframework.aot.AotDetector
2019
import org.springframework.beans.factory.ObjectProvider
20+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
2121
import org.springframework.beans.factory.config.BeanDefinition
2222
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
2323
import org.springframework.beans.factory.getBeanProvider
@@ -191,6 +191,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
191191
description?.let { bd.description = description }
192192
role?.let { bd.role = role.ordinal }
193193
order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) }
194+
bd.setAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true)
194195
}
195196

196197
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
@@ -237,6 +238,7 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
237238
description?.let { bd.description = description }
238239
role?.let { bd.role = role.ordinal }
239240
order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) }
241+
bd.setAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true)
240242
}
241243

242244

@@ -1199,9 +1201,6 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
11991201
* @param context The `ApplicationContext` to use for registering the beans
12001202
*/
12011203
override fun initialize(context: GenericApplicationContext) {
1202-
if (AotDetector.useGeneratedArtifacts()) {
1203-
return
1204-
}
12051204
this.context = context
12061205
init()
12071206
for (child in children) {

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

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThatExceptionOfType
2121
import org.junit.jupiter.api.Test
2222
import org.junit.jupiter.api.fail
2323
import org.springframework.beans.factory.NoSuchBeanDefinitionException
24+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
2425
import org.springframework.beans.factory.getBean
2526
import org.springframework.beans.factory.getBeanProvider
2627
import org.springframework.context.support.BeanDefinitionDsl.*
@@ -217,6 +218,25 @@ class BeanDefinitionDslTests {
217218

218219
assertThat(context.getBeanProvider<FooFoo>().orderedStream().map { it.name }).containsExactly("highest", "lowest")
219220
}
221+
222+
@Test
223+
fun `Declare beans with the functional Kotlin DSL flag them as to be ignored by AOT`() {
224+
val beans = beans {
225+
bean<Foo>("one")
226+
bean<Bar>("two")
227+
}
228+
229+
val context = GenericApplicationContext().apply {
230+
beans.initialize(this)
231+
}
232+
233+
assertThat(context.beanDefinitionNames).contains("one", "two")
234+
assertThat(context.getBeanDefinition("one").getAttribute(
235+
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
236+
assertThat(context.getBeanDefinition("two").getAttribute(
237+
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
238+
}
239+
220240
}
221241

222242
class Foo

0 commit comments

Comments
 (0)