liveBook · Manning

Chapter 4: The Spring context: Using abstractions

What you will learn in chapter 4 of Spring Start Here, Second Edition

This introduction explains why Spring applications use interfaces to decouple components and dependency injection to supply implementations.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why Spring applications use interfaces to decouple components and dependency injection to supply implementations.

Key terms: abstractions, interfaces, concrete implementations, dependency injection, Spring context, implementation

4.1 Using interfaces to define contracts

This section explains how Java interfaces state a responsibility without prescribing its implementation, so objects can express what they need rather than demand a particular solution.

Key terms: interface, responsibility, implementation, interfaces, what you need, solution

4.1.1 Using interfaces for decoupling implementations

The section shows how depending on a Java interface instead of a concrete class lets a printing object use different sorting implementations without changing the printer.

Key terms: DeliveryDetailsPrinter, SorterByAddress, concrete implementation, Sorter interface, decouple, implementation

4.1.2 The requirement of the scenario

Introduces a task-management comment feature whose publication triggers storage and an email notification.

Key terms: task, comment, configured address

4.1.3 Implementing the requirement without using a framework

This section builds a framework-free publish-comment example by separating responsibilities and making the service depend on interfaces rather than concrete implementations.

Key terms: use case, service, repository, proxy, CommentService, CommentRepository, interface, contract, abstractions, decouple

4.2 Using dependency injection with abstractions

Introduces Spring dependency injection with abstractions and explains why the topic matters, without yet teaching how to implement it.

Key terms: dependencies between objects, abstractions, dependency injection

4.2.1 Deciding which objects should be part of the Spring context

The section shows how to choose which objects Spring should manage and wire a comment-publishing service through interface-typed dependencies.

Key terms: Spring context, DI, Comment, @Component, @ComponentScan, stereotype annotations, CommentService, DBCommentRepository, EmailCommentNotificationProxy, CommentRepository

4.2.2 Choosing what to auto-wire from multiple implementations of an abstraction

This section explains how to resolve dependency injection when multiple Spring beans implement the same interface, using @Primary for a default or @Qualifier for a specific choice.

Key terms: Spring context, CommentNotificationProxy, NoUniqueBeanDefinitionException, @Primary, @Component, default, @Qualifier, CommentService, email, push notification

4.3 Focusing on object responsibilities with stereotype annotations

This section shows how @Service and @Repository make a Spring component’s responsibility explicit while still registering it in the context.

Key terms: @Component, @Service, @Repository, service, repository, stereotype annotations, Spring context

4.4 Questions

This section provides self-check questions on interfaces, dependency injection, Spring-managed objects, and stereotype annotations, with answers in Appendix A.

4.5 Summary

Summarizes how interfaces decouple classes and how Spring injects and identifies implementations of those interfaces.

Where this is used later

Concepts explained in this chapter

← Chapter 3: The Spring context: Wiring beans Chapter 5: The Spring context: Bean scopes and life cycle →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.