Spring Boot & Microservices (50 Qs)•Part 2: Persistence, Transactions & Security (11 - 24)
How do you prevent and resolve circular dependencies in Spring Boot?
mid LevelState ManagementSecurity & AuthDatabases & SQL
In Simple Words: A circular dependency occurs when Bean A requires Bean B, and Bean B requires Bean A:
- Best Fix (Architectural): Refactor code to extract shared logic into a third service (Bean C).
- Event-driven: Replace direct call with Spring ApplicationEvent.
- Lazy initialization: Annotate one injection with
@Lazy, causing Spring to inject a proxy rather than fully initializing both simultaneously. - Note: Spring Boot 2.6+ disables circular references by default.