Lesson 19 / 38
Spring & Dependency Injection
See how the Spring IoC container builds and injects your objects via constructor injection, and how Spring Boot auto-configures a runnable app from the classpath.
IoC container
Instead of a class creating its collaborators with new, the Spring container builds them and injects them (constructor injection preferred). This makes wiring configurable and code trivially testable with fakes/mocks.
Spring Boot
Boot auto-configures sensible defaults from the classpath, embeds a web server, and runs as a plain java -jar. Annotate a bean, declare its dependencies in the constructor, and Boot does the rest.
@Service
class OrderService {
private final OrderRepository repo;
OrderService(OrderRepository repo) { this.repo = repo; } // injected
}
@SpringBootApplication
public class App {
public static void main(String[] a) { SpringApplication.run(App.class, a); }
}