Beans & Dependency Injection
A bean is an object Spring creates and manages for you, and dependency injection means Spring hands those objects to the classes that need them.
What you will learn
- Define a bean in plain words
- Explain dependency injection with an example
- Use constructor injection
The problem: objects need other objects
In Java you often write new SomeClass() to build the objects you need. But in a real app, classes depend on many other classes, and building them all by hand gets messy and hard to change.
Spring solves this. It creates the objects for you, keeps them in one place, and gives them to whoever needs them. This is the core idea of the whole framework.
What is a bean?
A bean is simply an object that Spring creates and manages. You mark a class so Spring knows to manage it, and Spring builds one instance and keeps it ready. Common markers are @Service, @Component, @Repository and @RestController.
What is dependency injection?
Dependency injection (DI) means: instead of a class creating its own helpers with new, Spring injects (passes in) the ready-made beans it needs. The class just asks for them in its constructor.
Tip: Analogy: a chef does not grow the vegetables and raise the chickens before cooking. The ingredients are delivered ready to use, so the chef can focus on cooking. DI is the same: Spring delivers the ready-made objects (beans), so your class can focus on its real job instead of building its tools.
@Service
public class GreetingService {
public String greet(String name) {
return "Hello, " + name + "!";
}
}
@RestController
public class GreetingController {
private final GreetingService service;
// Spring sees this constructor and injects the GreetingService bean
public GreetingController(GreetingService service) {
this.service = service;
}
@GetMapping("/greet")
public String greet() {
return service.greet("Ashish");
}
}Note: Output (visiting /greet in a browser):
Hello, Ashish!
Notice we never wrote new GreetingService(). Spring created it as a bean and passed it into the controller for us — that is dependency injection.
Why this is better
| Without DI | With DI (Spring) |
|---|---|
Classes build their own helpers with new | Spring builds and shares the helpers |
| Hard to swap a helper for another | Easy — Spring decides what to inject |
| Harder to test | Easy to pass a fake in tests |
| You manage object lifetimes | Spring manages them |
Tip: Best practice: use constructor injection (ask for dependencies in the constructor, like above). It is clear, and the final field guarantees the dependency is always set.
Watch out: For Spring to inject a class, that class must be a bean (marked with @Service, @Component, etc.) and live in a scanned package. A plain class with no annotation will not be injected.
Q. What does “dependency injection” mean in Spring?
✍️ Practice
- Write a
MathServicebean with anadd(int a, int b)method and inject it into a controller. - Explain in one sentence why constructor injection is preferred over creating objects with
new.
🏠 Homework
- Create a
QuoteServicethat returns a random quote, mark it as a bean, and inject it into a controller method.