Simple spring-boot-starter-webmvc based application that has circular dependency bean beans created with BeanRgistar:
fun main(args: Array<String>) {
SpringApplicationBuilder(ExampleApp::class.java).build().run()
}
@EnableAutoConfiguration
@Import(ExampleRegistrar::class)
class ExampleApp
data class First(private val second: Second? = null)
data class Second(private val first: First? = null)
class ExampleRegistrar :
BeanRegistrarDsl({
registerBean("first") { First(second = bean<Second>()) }
registerBean("second") { Second(first = bean<First>()) }
registerBean() { router { GET("/test") { ServerResponse.ok().body("Works") } } }
})
starts correctly without reporting circular dependency problem and only prints suspicious logs:
2026-04-13T14:43:17.966+02:00 INFO 19916 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Bean 'first' marked for pre-instantiation (not lazy-init) but currently initialized by other thread - skipping it in mainline thread
2026-04-13T14:43:17.966+02:00 INFO 19916 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Bean 'second' marked for pre-instantiation (not lazy-init) but currently initialized by other thread - skipping it in mainline thread
Same is true for beans registered in initializer with GenericApplicationContext.
However when regular Configuration is used like this:
fun main(args: Array<String>) {
SpringApplicationBuilder(ExampleApp::class.java).build().run()
}
@EnableAutoConfiguration
@Configuration
open class ExampleApp {
@Bean open fun first(second: Second) = First(second = second)
@Bean open fun second(first: First) = Second(first = first)
@Bean open fun router() = router { GET("/test") { ServerResponse.ok().body("Works") } }
}
data class First(private val second: Second? = null)
data class Second(private val first: First? = null)
application start fails with properly reporting circular dependency problem:
**************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| first defined in example.ExampleApp
↑ ↓
| second defined in example.ExampleApp
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
Spring Boot 4.0.3
Simple
spring-boot-starter-webmvcbased application that has circular dependency bean beans created withBeanRgistar:starts correctly without reporting circular dependency problem and only prints suspicious logs:
Same is true for beans registered in
initializerwithGenericApplicationContext.However when regular
Configurationis used like this:application start fails with properly reporting circular dependency problem:
Spring Boot 4.0.3