Resolve a Circular Dependency in Spring

J_Learn
2 min readMay 7, 2023

--

Circular Dependency? when we’re working on large project with different services or modules we see commonly this issue. Before going to mention how to resolve it I would like to explain why occur this circular dependency issue.

Where occurring circular dependency issue

Circular Dependency

A Circular dependency is a condition in which two or more modules in a software system depend on each other in such a way as to create a dependency cycle. Circular dependencies can adversely affect cohesion because it can create a situation where elements within a module or component are too tightly coupled, making it difficult to separate them into smaller, more cohesive units. To maintain high cohesion and avoid circular dependencies, it is important to design software systems with clear and well-defined boundaries between modules and components and carefully manage dependencies between them.

How to Resolve Circular Dependency

There are several solutions to solve this problem and I will explain a solution with @Lazy annotation in Spring application. Because a large project makes it difficult to use critical changes that may break other functionality.

Source Code

According to the above two classes, circular dependency due to ServiceA and ServiceB is solved using @Lazy annotation.

--

--

No responses yet