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() } }