When an ApplicationContext
is optimized by Spring AOT, there are a number of limitations. One of them is that the bean registration cannot use an instance supplier as it isn't possible for AOT to generate a bean registration that calls it.
https://github.com/spring-projects/spring-framework/issues/29555 is a concrete example of this problem where the use of the Kotlin DSL makes it so that it fails out of-the-box.
Certain callbacks are execute twice at the moment, once when the context is created ahead-of-time for it to be processed and once more at runtime because the component has been registered. When we have this flexibility, it would be nice to instruct AOT to ignore the bean registration. Adding something like AOT_PROCESSING_IGNORE_REGISTRATION
on the bean definition could do it.
This way the bean definition won't be contributed and won't suffer that limitation. At runtime, the callback that is invoked would create the bean.
Comment From: snicoll
This wasn't really hard to implement and it shed some light on BeanRegistrationExcludeFilter
that could just as well prepare the context by setting the attribute now we're fairly confident that attributes aren't persisted in AOT-processed bean definitions.
I've tried to spike a bit how that would be used and the Kotlin DSL does not seem to keep track of what it does. If we update the DSL to set the attribute on the bean definitions that it generates then nothing is being contributed (as everything is skipped) and we no longer call the DSL at runtime.
I wonder if someone with a good understanding of the Kotlin DSL could help me understand how we could invoke that code again.
Comment From: snicoll
When merging this, another look at https://github.com/spring-projects/spring-boot/issues/34563 to remove the filter should be helpful.
Comment From: snicoll
I've found out what's going on and have a working sample. The initializer disables itself when it run in AOT mode, see https://github.com/spring-projects/spring-framework/issues/29211 - The situation describe in that issue was broken as, back then, we were not checking for instance suppliers.