Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
IoC and DI | Spring Boot Basics
Spring Boot Backend
course content

Course Content

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

IoC and DI

The Spring Container is the central component of the Spring Framework that manages the creation, configuration, and lifecycle of beans (objects) within an application. The Spring Container is also known as the IoC container.

How the Spring container works?

When the application starts, the Spring Container is initialized. It begins by scanning the classes for annotations like @Component, @Service, @Repository, and @Configuration. By marking a class with one of these annotations, you ensure that it becomes a bean within the Spring context.

Once the classes are identified, the container proceeds to create instances of these annotated classes, effectively turning them into beans that the Spring context manages throughout the application's lifecycle.

As these beans are created, the container automatically handles their dependencies through Dependency Injection. This process allows classes to receive the necessary dependencies without needing to explicitly create or locate them, streamlining the development process.

Dependency Injection (DI)

First, the Spring Container adds to its context the beans (classes) marked with the annotations @Component, @Service, @Repository, and @Configuration. After this, it provides these beans to any objects that request them.

Real Life Example

Imagine you're developing an online store. You have a class OrderService that manages orders and a class PaymentService that handles payments. Instead of manually creating an instance of PaymentService inside OrderService, you can let the Spring Container create and inject PaymentService into OrderService.

Note

You can switch between classes in the bar at the top.

java

PaymentService

java

OrderService

copy
1234567
@Service public class PaymentService { public void processPayment() { System.out.println("Processing payment..."); } }

In the OrderService class, we will have a field paymentService that is initialized with the exact type we need (PaymentService). Notice that we did not explicitly initialize this object, Spring did it for us!

If we had not annotated PaymentService with @Service, this bean would not have been added to the Spring context, and we would have encountered an error indicating that Spring could not find such a bean.

@Autowired

You can also use the @Autowired annotation, which tells the Spring Container to inject the appropriate object into the field.

java

OrderService

copy
123456789101112131415
@Service public class OrderService { private final PaymentService paymentService; @Autowired public OrderService(PaymentService paymentService) { this.paymentService = paymentService; } public void placeOrder() { System.out.println("Placing order..."); paymentService.processPayment(); } }

In Spring, starting from version 4.3, using the @Autowired annotation is no longer required when injecting dependencies via a constructor if there is only one constructor in the class. Spring automatically determines that this constructor should be used for dependency injection.

What are the Different Types of Bean?

Imagine you have a single coffee machine at a cafе. Whenever a customer orders a coffee, the barista uses this same coffee machine to prepare each cup. The coffee machine is always the same one, not a new one for every order.

Similarly, in Spring, when you inject a bean from the Spring context, Spring provides you with the same instance of that bean every time. This is known as the singleton scope. Just like the coffee machine at the cafe, Spring ensures that you always get the same object rather than creating a new one each time.

Singleton

We can set the type using the @Scope annotation and specify singleton in the attributes.

SingletonService

SingletonService

copy
12345
@Service @Scope("singleton") public class SingletonService { // Class for handling business logic }

Note

However, it's not necessary to specify the scope as singleton because it is the default in Spring.

Prototype

java

PrototypeService

copy
12345
@Service @Scope("prototype") public class PrototypeService { // Class for handling business logic }

We do the same thing, but specify prototype in the attributes. This ensures that each time we inject this bean, it will return a new object.

Note

There are also some more scope beans, but we will not consider them here.

Summary

In this chapter on IoC (Inversion of Control) and DI (Dependency Injection), we explored how Spring manages the creation and lifecycle of beans through its container, enabling automatic dependency injection.

We discussed the default singleton scope, where a single instance of a bean is reused, and the prototype scope, which creates a new instance for each request, highlighting how these scopes impact object management and application design.

1. What is `Inversion of Control` (IoC) in Spring?
2. What principle underlies `Dependency Injection` (DI)?

What is Inversion of Control (IoC) in Spring?

Select the correct answer

What principle underlies Dependency Injection (DI)?

Select the correct answer

Everything was clear?

Section 2. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt