Spring Framework· 35 min

Spring IoC and Dependency Injection

Understand the container, dependency injection, constructor injection, component scanning, and testable design.

Java interview lesson
Question 16 · Not reviewed

Spring · fundamentals

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Spring IoC and Dependency Injection

Explain it aloud in your own words

Strong answer

Spring's container creates and wires objects according to configuration. Dependency injection makes collaborators explicit, keeps business logic independent from construction details, and allows tests to provide fakes. Constructor injection is usually the clearest default because required dependencies cannot be missing.

Key points

  • A bean is an object managed by the Spring container.
  • The container manages wiring; it should not contain business rules.
  • Prefer interfaces only where they represent a meaningful boundary.
  • Constructor injection makes immutable dependencies and unit tests straightforward.

Interviewer looks for

Explain the difference between the object graph and the business logic that uses it.

Java example

1@Service
2final class OrderService {
3  private final OrderRepository orders;
4
5  OrderService(OrderRepository orders) {
6    this.orders = orders;
7  }
8}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

What do IoC and dependency injection mean in Spring?

Strong answer

Inversion of control means application object creation and wiring are managed by the container. Dependency injection supplies collaborators to an object instead of making the object construct them directly.

Q2Intermediate

Why is constructor injection usually preferred?

Strong answer

Required dependencies are explicit, can be final, and must be provided before the object is usable. Constructor injection also makes a class straightforward to unit test without starting Spring.

Q3Intermediate

What does component scanning do?

Strong answer

Component scanning finds classes annotated as components within configured packages and registers bean definitions for them. Package boundaries should be intentional so scanning does not create accidental dependencies.

Q4Intermediate

What is the difference between a Spring bean and a component?

Strong answer

A bean is any object managed by the application context. A component is a class discovered through component scanning, while @Bean methods and framework configuration can also create beans.

Q5Intermediate

How would you unit test a service that uses dependency injection?

Strong answer

Instantiate the service directly with fake or mocked collaborators, then test business behavior independently of container startup. Use integration tests separately to verify wiring and framework behavior.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.