Skip to content

Commit

Permalink
Refine "Add AOT/Native support"
Browse files Browse the repository at this point in the history
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
  • Loading branch information
snicoll committed Jul 29, 2024
1 parent b888f36 commit 2596e29
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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()
}

}
Expand Down

0 comments on commit 2596e29

Please sign in to comment.