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
This commit is contained in:
Stéphane Nicoll
2024-07-24 13:58:36 +02:00
parent e741d6edbb
commit b8f9913aa1
2 changed files with 24 additions and 5 deletions

View File

@@ -21,6 +21,7 @@ import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.fail
import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
import org.springframework.beans.factory.getBean
import org.springframework.beans.factory.getBeanProvider
import org.springframework.context.support.BeanDefinitionDsl.*
@@ -217,6 +218,25 @@ class BeanDefinitionDslTests {
assertThat(context.getBeanProvider<FooFoo>().orderedStream().map { it.name }).containsExactly("highest", "lowest")
}
@Test
fun `Declare beans with the functional Kotlin DSL flag them as to be ignored by AOT`() {
val beans = beans {
bean<Foo>("one")
bean<Bar>("two")
}
val context = GenericApplicationContext().apply {
beans.initialize(this)
}
assertThat(context.beanDefinitionNames).contains("one", "two")
assertThat(context.getBeanDefinition("one").getAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
assertThat(context.getBeanDefinition("two").getAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
}
}
class Foo