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

IoC and DI

Veeg om het menu te tonen

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.

Note
Definition

Inversion of Control (IoC) is when Spring takes care of creating the objects you need and connecting them together for you.

A dependency refers to any class that requires other objects to be injected for it to function properly, and this chain of dependencies can go on indefinitely. Normally, to create an object with dependencies, we have to explicitly initialize it using the new operator and specify the class.

But that’s where Spring comes in with Inversion of Control and Dependency Injection. The idea is that Spring takes over the responsibility of creating and managing dependencies. Instead of manually handling the setup, we simply annotate the necessary classes, and Spring automatically creates them and manages their lifecycle within its context.

One of these annotations is @Component, which we place above the class.

BeanExample.java

BeanExample.java

This approach shifts the focus away from manually managing dependencies, allowing developers to concentrate on business logic while Spring handles the infrastructure tasks related to object creation and dependency management.

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.

@Component is a general annotation that makes a class managed by Spring, allowing it to be registered as a bean.

@Service, @Repository, and @Controller are more specialized versions of @Component. They serve the same purpose but indicate the specific role of the class: @Service for business logic, @Repository for data access, and @Controller for handling web requests.

@Configuration is used for classes that contain bean definitions and application configuration.

By marking a class with one of these annotations, you ensure that it becomes a bean within the Spring context.

spring+annotation

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.

lifecyclebean
Note
Note

A bean is removed from the Spring context when the container shuts down.

Dependency Injection (DI)

Note
Definition

DI (Dependency Injection) is a technique that automatically injects dependencies into a class. This reduces the coupling between components and makes the code easier to test and modify.

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
Note

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

OrderService.java

OrderService.java

PaymentService.java

PaymentService.java

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.

OrderService.java

OrderService.java

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

Note
Definition

Singleton is the default scope in Spring, where only one instance of the bean is created per container. This bean is created when the container starts and exists until the container is shut down, making it suitable for objects that need to be unique and reusable throughout the entire application.

singleton+spr

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

SingletonService

SingletonService

Note
Note

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

Prototype

Note
Definition

Prototype means that a new instance of the bean is created each time it is requested. The bean exists only until it is used, making it suitable for objects that need to be created and destroyed as needed.

property+spr
PrototypeService.java

PrototypeService.java

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
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)?

question mark

What is Inversion of Control (IoC) in Spring?

Selecteer het correcte antwoord

question mark

What principle underlies Dependency Injection (DI)?

Selecteer het correcte antwoord

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 2. Hoofdstuk 4
some-alt