From 2596e29013db61474df1c180e459f4330d4beeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 29 Jul 2024 08:49:28 +0200 Subject: [PATCH] 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 --- .../springframework/context/support/BeanDefinitionDsl.kt | 6 +++++- .../context/support/BeanDefinitionDslTests.kt | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index ab34b4ba62..89c607a600 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -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) diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index 544c93ffba..11bb65c8a9 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -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("one") + bean("one") { foo() } bean("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() } }