Reader small image

You're reading from  Full Stack Development with Spring Boot 3 and React - Fourth Edition

Product typeBook
Published inOct 2023
PublisherPackt
ISBN-139781805122463
Edition4th Edition
Right arrow
Author (1)
Juha Hinkula
Juha Hinkula
author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula

Right arrow

Understanding Dependency Injection

In this chapter, we will learn what dependency injection (DI) is and how we can use it with the Spring Boot framework. The Spring Boot framework provides DI; therefore, it is good to understand the basics. DI allows for loose coupling between components, making your code more flexible, maintainable, and testable.

In this chapter, we will look into the following:

  • Introducing dependency injection
  • Using dependency injection in Spring Boot

Technical requirements

Before you begin: Join our book community on Discord

Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the "full-stack-dev-spring-boot-3-react-4e" channel under EARLY ACCESS SUBSCRIPTION).

https://packt.link/EarlyAccess

Qr code Description automatically generated

In this chapter, we will learn what Dependency Injection (DI) is and how we can use it with the Spring Boot framework. The Spring Boot framework provides DI; therefore, it is good to understand the basics. DI reduces component dependencies and makes your code easier to test and maintain. It allows for loose coupling between components, making your code more flexible, maintainable, and testable.In this chapter, we will look into the following:

  • Introducing DI
  • Using DI with Spring Boot

Technical requirements

Java SDK version 17 or higher is necessary to use with Spring Boot 3. In this book, we are using Windows, but all the tools are available for Linux and macOS as well.All of the code for this chapter can be found at the following GitHub link: https://github.com/PacktPublishing/Full-Stack-Development-with-Spring-Boot-3-and-React-Fourth-Edition/tree/main/Chapter02

Introducing DI

DI is a software development technique where we can create objects that depend on other objects. DI helps with interaction between classes but at the same time keeps the classes independent.There are three types of classes in DI:

  • A service is a class that can be used (this is the dependency).
  • The client is a class that uses dependency.
  • The injector passes the dependency (the service) to the dependent class (the client).

The three types of classes in DI are shown in the following diagram:

Figure 2.1: Dependency Injection classes

DI makes classes loosely coupled. This means that the creation of client dependencies is separated from the client's behavior, which makes unit testing easier.Let's take a look at a simplified example of DI using Java code. In the following code, we don't have DI, because the client Car class is creating an object of the service class:

public class Car {
  private Owner owner;
  
  public Car() {
    owner = new Owner();
}
}
...

Using DI in Spring Boot

In Spring framework, the dependency injection is achieved through the Spring Application Context. Application context is responsible for creating and managing objects (beans) and their dependencies. Spring Boot scans your application classes and registers classes with certain annotations (@Service, @Repository, and @Controller) as Spring beans. These beans can then be injected using an @Autowired annotation:

public class Car {
  @Autowired
  private Owner owner;
  ...
}

A fairly common situation is where we need database access for some operations, and, in Spring Boot, we use repository classes for that. In this situation, we can inject the repository class and start to use its methods:

public class Car {
  @Autowired
  private CarRepository carRepository;
    // Fetch all cars from db 
carRepositoty.findAll();
...
}

Jakarta EE (jakarta.annotation) also provides a @Resource annotation that can be used for injections. You can define the name or type of the injected...

Summary

In this chapter, we learned what DI is and how to use DI in the Spring Boot framework, which we are using in our backend. It’s good to understand the basics of dependency injection and how it’s implemented in Spring framework.In the next chapter, we will look at how we can use the Java Persistent API (JPA) with Spring Boot and how to set up a MariaDB database. We will also learn about the creation of CRUD repositories and the one-to-many connection between database tables.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full Stack Development with Spring Boot 3 and React - Fourth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781805122463
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Juha Hinkula

Juha Hinkula is a software development lecturer at Haaga-Helia University of Applied Sciences in Finland. He received an MSc degree in Computer Science from the University of Helsinki and he has over 17 years of industry experience in software development. Over the past few years, he has focused on modern full stack development. He is also a passionate mobile developer with Android-native technology, and also uses React Native.
Read more about Juha Hinkula