Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Full Stack Development with Spring Boot and React - Third Edition

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

Product type Book
Published in Apr 2022
Publisher Packt
ISBN-13 9781801816786
Pages 378 pages
Edition 3rd Edition
Languages
Author (1):
Juha Hinkula Juha Hinkula
Profile icon Juha Hinkula

Table of Contents (22) Chapters

Preface 1. Part 1: Backend Programming with Spring Boot
2. Chapter 1: Setting Up the Environment and Tools – Backend 3. Chapter 2: Understanding Dependency Injection 4. Chapter 3: Using JPA to Create and Access a Database 5. Chapter 4: Creating a RESTful Web Service with Spring Boot 6. Chapter 5: Securing and Testing Your Backend 7. Part 2: Frontend Programming with React
8. Chapter 6: Setting Up the Environment and Tools – Frontend 9. Chapter 7: Getting Started with React 10. Chapter 8: Consuming the REST API with React 11. Chapter 9: Useful Third-Party Components for React 12. Part 3: Full Stack Development
13. Chapter 10: Setting up the Frontend for Our Spring Boot RESTful Web Service 14. Chapter 11: Adding CRUD Functionalities 15. Chapter 12: Styling the Frontend with React MUI 16. Chapter 13: Testing Your Frontend 17. Chapter 14: Securing Your Application 18. Chapter 15: Deploying Your Application 19. Chapter 16: Best Practices 20. Assessments 21. Other Books You May Enjoy

Chapter 2: 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 reduces component dependencies and makes your code easier to test and maintain.

In this chapter, we will look into the following:

  • Introducing DI
  • Using DI with Spring Boot

Technical requirements

Java SDK version 8 or higher is necessary to use Eclipse IDE. 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-and-React/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 the 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 – DI

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...

Using DI in Spring Boot

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();
  ...
}

Java (javax.annotation) also provides an @Resource annotation that can be used to inject resources. You can define the name or type of the injected bean when using the @Resource annotation. For example, the following code shows some use cases. Imagine that we have...

Summary

In this chapter, we learned what DI is. We also learned how to use DI in the Spring Boot framework, which we are using in our backend.

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.

Questions

  1. What is DI?
  2. How does the @Autowired annotation work in Spring Boot?
  3. How do you inject resources in Spring Boot?

Further reading

Packt has other great resources for learning about Spring Boot:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Full Stack Development with Spring Boot and React - Third Edition
Published in: Apr 2022 Publisher: Packt ISBN-13: 9781801816786
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.
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}