Introduction to Spring Framework & Spring Boot
Learning Objectives
Spring is one of the most widely used frameworks in Java development. It provides a comprehensive programming and configuration model for modern enterprise applications. Understanding Spring is essential for building scalable, maintainable, and testable applications, because many applications depend on it for tasks like dependency management, configuration, and testing.
Spring Boot is a tool built on top of Spring that makes it easier to create stand-alone, production-ready applications with minimal configuration. It removes boiler-plate code and provides sensible defaults, which reduces setup time and complexity. Spring Boot provides tools like Spring Initializr, spring profiles, and environment configuration to streamline development and deployment. These features allow developers to quickly scaffold projects, manage environments, and reduce errors during deployment.
There is a variety of reading material provided here, it’s not essential to read it all, some covers similar content from a different point of view.
Self Study
Reading material
- What are Java Frameworks?
- Introduction to Spring Framework
- Introduction to Inversion of Control and Dependency Injection with Spring
- Why is field injection not recommended?
- How Spring Boot compares to Spring
- Boot strap a simple application
- Spring boot annotations
Framework documentation
These pages may be a useful first port of call when troubleshooting, but you don’t need to read them in their entirety:
Exercises
✍️Exercise 1.1
Here we are going to generate a to do list application, using spring initializr, that we will build on throughout the sprint. In this section, we’ll set up the building blocks and initial services:
- Generate a spring application with the following dependencies:
- spring-boot-starter-web
- spring-boot-starter-data-jpa
- spring-boot-starter-test
- Create as a maven project, using the latest version of maven available.
- Create models for the following:
- Task - each task should have the following: title, priority, completed, due date, assignee username
- User - each user should have an id and username
- Create a
TaskServicewith the following methods, writing unit tests for each: createTask- Pass in a task without an id. The method will provide a new id, validate and store the new task.
listTasksupdateTaskById- Provide a task with an id (a task without id will fail), update the task (unit test should validate this list is the same size).
deleteTaskByIdgetTasksByUser- Add a
TaskValidatorServicewhich is able to validate a number of cases, including:- Title validation: short, long, null, empty, incl. spaces.
- Write a unit test for each case.
Quiz
- Which of these best describes how the Spring Framework helps a developer build an application?
- It acts as a “glue” that manages how different objects (dependencies) interact with each other
- It provides specialised modules to help with tasks like Database access.
- It removes the need for boiler-plate code
- All of the above
- Which annotation is used to indicate a Spring-managed bean?
@Component@Bean@Service- All of the above
- What is dependency injection in Spring?
- The process of creating an object
- A design pattern for managing object creation
- A way to achieve loose coupling between classes
- Both 2 and 3
- What annotation is used for automatic dependency injection in Spring?
@Inject@Autowired@Resource@Qualifier
Answers
- 4: Spring acts as comprehensive toolbox that manges how objects interact, provides pre-built modules and eliminates repetitive setup code in favour of an annotation-driven approach
- 4: All these annotations indicate a Spring-managed bean.
@Componentis a generic stereotype, while@Serviceis used for service-layer beans, and @Bean is used in configuration classes.
- 4: All these annotations indicate a Spring-managed bean.
- 4: Dependency injection is a design pattern that allows for loose coupling between classes by managing object creation and dependencies outside of the classes themselves.
- 2: The
@Autowiredannotation is used to tell Spring to automatically find and inject dependencies into a field, constructor or setter, without needing to instantiate objects using the new keyword
- 2: The
Introducing Java Persistence API (JPA) and Spring Data
Learning Objectives
Most enterprise apps need persistent data (i.e. data that remains after the application/process that created it shuts down). Java Persistence API (JPA) provides a standard for object-relational mapping (ORM) (i.e. JPA defines how Java objects are mapped to database data). Spring Data JPA is built on top of this to reduce boilerplate code, and makes database access simple and consistent.
In a Spring application @Entity annotated classes represent data stored in the database. These are simple Java classes where each field in the class corresponds to a column in the table. Fields may be marked with annotations such as @Id and @Column.
For example, the following Entity represents an Employee table with three columns, id, name and email.
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
// Constructors, getters, and setters
}@Repository annotated interfaces define how the data in the database is queried and persisted. Spring automatically generates Create, Read, Update, Delete (CRUD) operations. By following specific naming conventions, Spring Data JPA automatically generates queries at runtime, we call these Query Methods. Query methods are repository interface methods where the query is derived from the method name.
For example:
List<Entity>; findByFieldName(DataType fieldName);Self Study
Reading Material
Exercises
✍️Exercise 2.1
- Create a new database, which has User and Task tables. One User can have more than one Task assigned to them.
- Extract TaskService into an interface, and create a database backed implementation.
Quiz
- Which of the following annotations is used for defining a repository bean?
@Service@Component@Repository@Controller
Answers
1 - C: The @Repository annotation is specifically used to mark a class as a Data Access Object (DAO) or repository.
Importance: This introduces persistence, which is essential for real-world applications like e-commerce, social media, and enterprise systems.
Introduction to Spring REST
Learning Objectives
REST stands for Representational State Transfer and allows applications to communicate using HTTP methods, for example GET, POST, PUT and DELETE. REST APIs follow a stateless architecture, i.e. every request contains all the necessary information for the server to process it without relying on previous requests.
Spring REST is part of the Spring Framework, which simplifies the process of writing REST APIs by providing support for handling HTTP requests, data conversion & error handling. To utilise Spring REST, we must have a dependency on Spring WEB (spring-boot-starter-web) in our project.
REST Controllers manage HTTP requests, to define a REST Controller, we use the @RestController spring annotation.
Postman is a popular HTTP client used for sending HTTP requests to a REST API and checking the response, allowing you to test out your newly created APIs.
Reading material
Exercises
✍️Exercise 3.1
- Create the following REST controllers, with the listed methods:
TaskController- Get all tasks from the database
- Create a new task and save it to the database
- Delete a task from the database
UserController- Get all users from the database
- Test using Postman or curl (or alternative tooling for testing HTTP calls).
Testing in Spring Boot
Learning Objectives
Testing ensures that your application is reliable, maintainable, and behaves the way you expect. Spring Boot provides powerful, built‑in support for testing through JUnit and Mockito, making it easy to test everything from small pieces of logic to full application flows. In this section, students will learn the difference between unit and integration tests, how to write each type using Spring Boot’s testing tools, and how to use Mockito to isolate components with mocks. By the end, they’ll have a solid foundation in verifying Spring applications efficiently and consistently.