Spring_Core_Interview_Problems
Spring_Core_Interview_Problems
Cloud)
How does Spring's Dependency Injection (DI) work and what are the main
types?
Spring DI allows objects to be injected into other objects, promoting loose coupling.
Types of DI in Spring:
Constructor Injection — recommended for mandatory dependencies.
Setter Injection — useful for optional dependencies.
Field Injection — not recommended for testability and immutability reasons.
DI can be configured via XML, Java config (@Configuration + @Bean), or annotations
(@Component, @Autowired).
How does Spring handle circular dependencies and what are the limitations?
Spring can resolve circular dependencies for singleton beans using setter injection.
Mechanism:
Spring creates early references for beans during instantiation and wires them later.
Limitations:
Constructor-based injection with circular dependencies results in a
BeanCurrentlyInCreationException.
Prototype-scoped circular dependencies are not supported at all.
Best practices:
Refactor design to eliminate tight circular dependencies.
Use setter injection carefully if circular structure is unavoidable.