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
This commit is contained in:
Stéphane Nicoll 2024-07-29 08:49:28 +02:00
parent b888f362e5
commit 2596e29013
2 changed files with 8 additions and 4 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.context.support
import org.springframework.aot.AotDetector
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
import org.springframework.beans.factory.config.BeanDefinition
@ -181,6 +182,10 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
role: Role? = null,
order: Int? = null) {
// This version registers a regular bean definition that has been processed by AOT.
if (AotDetector.useGeneratedArtifacts()) {
return
}
val customizer = BeanDefinitionCustomizer { bd ->
scope?.let { bd.scope = scope.name.lowercase() }
isLazyInit?.let { bd.isLazyInit = isLazyInit }
@ -191,7 +196,6 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
description?.let { bd.description = description }
role?.let { bd.role = role.ordinal }
order?.let { bd.setAttribute(AbstractBeanDefinition.ORDER_ATTRIBUTE, order) }
bd.setAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true)
}
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context)

View File

@ -220,9 +220,9 @@ class BeanDefinitionDslTests {
}
@Test
fun `Declare beans with the functional Kotlin DSL flag them as to be ignored by AOT`() {
fun `Declare beans flag them as to be ignored by AOT if needed`() {
val beans = beans {
bean<Foo>("one")
bean("one") { foo() }
bean<Bar>("two")
}
@ -234,7 +234,7 @@ class BeanDefinitionDslTests {
assertThat(context.getBeanDefinition("one").getAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
assertThat(context.getBeanDefinition("two").getAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isEqualTo(true)
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE)).isNull()
}
}