Circular dependencies and how to avoid them
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
recognize and remove a cycle.
Planned topics
- A needs B and B needs A: with constructors Spring cannot build either, and fails at startup.
@Lazycan hide the cycle but not fix the design.- A cycle means responsibilities are tangled.
- Fix: extract the shared part into a third class, or move the call to an event or a method parameter.
Draft outline details
Demonstration
OrderService ⇄ InventoryService; extract StockPolicy that both use.
Check your understanding
Why is @Lazy a poor fix?
Show answer
It hides a design problem instead of removing it.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.