Skip to content

Circular dependency between supplier-created beans is silently ignored on startup #36725

@hajdamak

Description

@hajdamak

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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions