Sub topic 1
Background: Spring Framework and Dependency Injection
Prerequisites
This chapter assumes you know the following:
- Basic Java syntax: classes, interfaces, constructors, and annotations.
- What a build tool such as Maven or Gradle does, at a user level.
- A basic idea of HTTP and web applications.
- A JDK is installed and a terminal is available.
You don't need any Spring experience. This chapter covers the one idea that the rest of the tutorial builds on: dependency injection.
What the Spring Framework Is
The Spring Framework is a Java library for building applications out of many cooperating objects. Its core is a container that creates your objects, connects them to each other, and manages their lifecycle. Other Spring modules add web support, data access, security, and testing on top of that core.
Spring Boot, the topic of this tutorial, sits on top of the Spring Framework. To see what Boot saves you, you first need to see what the framework does.
The Problem: Objects That Build Their Own Dependencies
A dependency is an object that another object needs in order to work. In the next example, OrderService needs a PaymentGateway. It creates one itself.
public class OrderService {
private final PaymentGateway gateway = new CardPaymentGateway();
public void checkout(double amount) {
gateway.charge(amount);
}
}This code works, but OrderService is now tied to CardPaymentGateway. To test it with a fake gateway, you would have to edit the class. To switch providers, you would have to find every place that calls new.
Dependency Injection
Dependency injection (DI) means an object receives its dependencies from the outside instead of creating them. The simplest form is constructor injection.
public interface PaymentGateway {
void charge(double amount);
}
public class CardPaymentGateway implements PaymentGateway {
@Override
public void charge(double amount) {
System.out.println("Charging " + amount + " by card");
}
}
public class OrderService {
private final PaymentGateway gateway;
public OrderService(PaymentGateway gateway) {
this.gateway = gateway;
}
public void checkout(double amount) {
gateway.charge(amount);
}
}OrderService now depends only on the PaymentGateway interface. A test can pass in a fake. Production code can pass in the real one. Something still has to create the objects and pass them in. In plain Java, you would write that wiring by hand in main. In Spring, the container does it.
The Spring Container and Beans
A bean is an object that the Spring container creates and manages. The container is often called the application context. It reads a description of your beans, creates them in the right order, and injects each one into the objects that need it.
You mark a class as a bean with an annotation such as @Component. Spring finds these classes by scanning packages.
import org.springframework.stereotype.Component;
@Component
public class CardPaymentGateway implements PaymentGateway {
@Override
public void charge(double amount) {
System.out.println("Charging " + amount + " by card");
}
}import org.springframework.stereotype.Service;
@Service
public class OrderService {
private final PaymentGateway gateway;
public OrderService(PaymentGateway gateway) {
this.gateway = gateway;
}
public void checkout(double amount) {
gateway.charge(amount);
}
}@Service is a specialized @Component. When the container builds OrderService, it sees that the constructor needs a PaymentGateway. It finds the CardPaymentGateway bean and passes it in. You never write new for either class.
Why This Matters for Spring Boot
Before Spring Boot, you had to tell the container which beans to create and which libraries to wire together. That meant long XML files or Java configuration classes. For a web application you also had to choose compatible library versions and set up a server to run it.
Spring Boot keeps the same container and the same DI model. It removes most of that setup. The next chapters show how.
Common Errors
- No qualifying bean. Spring reports that no bean of type
PaymentGatewayexists. The usual cause is a missing annotation or a class outside the scanned packages. - More than one candidate. If two classes implement
PaymentGatewayand both are beans, Spring can't choose. You resolve this with@Primaryor@Qualifier. - Creating a bean with
new. An object you create yourself is not managed by the container. Its dependencies are not injected.
Exercise
Why is constructor injection easier to test than creating a dependency with new inside the class?
Show answer
With constructor injection, a test can pass a fake or stub implementation of the interface into the constructor. With new inside the class, the concrete dependency is fixed, so you can't replace it without editing the class.
Exercise
Two classes implement PaymentGateway, and both are annotated with @Component. What happens when Spring builds OrderService, and how can you fix it?
Show answer
Spring fails because it can't decide which bean to inject. Mark one class with @Primary, or select one by name with @Qualifier on the constructor parameter.