Stop 14 · Spring Boot & Backend DevelopmentTopics · 2/13

Spring Core & Dependency Injection

Part of the Java Roadmap

In short

Spring's big idea is simple: your classes shouldn't create their own helpers. Spring builds them and hands them in. Once this clicks, every annotation in Spring Boot starts to make sense.

Spring manages your objects (beans) and injects their dependencies for you, which keeps classes loosely coupled and easy to test. Constructor injection is the recommended style.

The jaadu as a wedding caterer hands baskets of vegetables and spices to a busy cook at a big outdoor kitchen decorated with marigold garlands.

Think of it like this

The wedding caterer

At a big wedding in Jaipur, the cook doesn't go to the market for vegetables or hire his own helpers. The caterer arranges everything and hands the cook exactly what he needs. The cook just cooks. The Spring container is the caterer, your classes are the cooks, and the ingredients handed over are injected dependencies.

Watch it happen

What the container does at startup

Spring container
OrderRepository
EmailService
OrderService
scan

1/5Spring scans your packages for @Component, @Service, @Repository and @Configuration classes.

The big ideas

What you need to know

Beans and the container

A bean is an object Spring creates and manages. The ApplicationContext is the container that holds them.

Stereotype annotations

@Component is generic. @Service, @Repository and @Controller are specialised versions that also signal the layer.

Constructor injection

Declare dependencies as final fields set by the constructor. With one constructor, no @Autowired is needed.

Singleton by default

Each bean is created once and shared. Keep beans stateless, or use another scope like prototype on purpose.

See it in code

Walk through the code

java
1import org.springframework.stereotype.Repository;2import org.springframework.stereotype.Service;3 4public interface NotificationSender {5    void send(String to, String message);6}7 8@Service9class SmsSender implements NotificationSender {10    public void send(String to, String message) {11        System.out.println("SMS to " + to + ": " + message);12    }13}14 15@Repository16class OrderRepository {17    long save(String item) { return 101L; }18}19 20@Service21public class OrderService {22    private final OrderRepository repo;23    private final NotificationSender sender;24 25    public OrderService(OrderRepository repo, NotificationSender sender) {26        this.repo = repo;27        this.sender = sender;28    }29 30    public void placeOrder(String phone, String item) {31        long id = repo.save(item);32        sender.send(phone, "Order " + id + " confirmed");33    }34}

1/4Depend on an interface, not a concrete class.

OrderService depends on an interface. Swap SmsSender for a WhatsApp sender and OrderService doesn't change.

Save yourself the pain

Common mistakes

Oops

Using @Autowired on private fields everywhere.

Do this instead

Prefer constructor injection. It makes dependencies obvious, allows final fields and lets you test with plain new.

Oops

Writing new OrderService(...) yourself inside another bean.

Do this instead

That object isn't managed by Spring, so its own dependencies won't be injected. Ask for it through the constructor.

Oops

Two beans implement the same interface and the app fails to start.

Do this instead

Mark one with @Primary, or choose with @Qualifier("beanName") at the injection point.

Your mission

Swap the sender

~25 min

Quick check

Why is constructor injection preferred over field injection?

Go deeper

  1. 1Explain inversion of control and dependency injection in your own words.
  2. 2Use @Component, @Service and @Repository with constructor injection.
  3. 3Write a unit test that passes a mock into a service's constructor.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge