Spring / Spring7 basics Interview questions
What are the types of Dependency Injection in Spring?
Spring supports three main styles of injecting dependencies into a bean, and picking the right one affects testability and immutability.
| Type | How it works |
| Constructor injection | Dependencies passed as constructor arguments; recommended default, supports immutable, non-null fields |
| Setter injection | Dependencies assigned via setter methods; useful for optional dependencies |
| Field injection | @Autowired applied directly to a field; concise but harder to test and discouraged for production code |
Spring's own team recommends constructor injection as the default since it makes dependencies explicit and required, allows fields to be marked final, and works cleanly with plain unit tests that don't need a container.
More Related questions...