Dependency Injection and Inversion of Control (IoC) Explanations with Example

Dependency Injection (DI) is a design pattern that is commonly used in Spring Boot applications to provide loose coupling between different components of the application. The main idea behind DI is to allow the components of an application to be loosely coupled, so that they can be tested, maintained and updated independently of each other.

In DI, dependencies are “injected” into a class or object, rather than being instantiated within that class or object. This means that the class or object can be decoupled from its dependencies, and therefore can be more easily tested and reused.

Here’s an example of DI in action:

Suppose you have a class called “UserService” that needs to use a database to store user information. Rather than creating a new database connection within the UserService class, you can use DI to inject the database connection as a dependency. This allows the UserService class to be loosely coupled with the database, so that it can be tested and maintained independently.

Here’s an example of how you might implement DI in Spring Boot:

@Service
public class UserService {
    
    private final UserRepository userRepository;
    
    @Autowired
    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
    
    public User createUser(User user) {
        return userRepository.save(user);
    }
}

In this example, the UserRepository dependency is injected into the UserService class using the @Autowired annotation. This means that the UserService class can use the UserRepository without knowing anything about how it is implemented, allowing for greater flexibility and easier testing.

Inversion of Control (IoC) is a related design pattern that is also commonly used in Spring Boot applications. IoC is a way of implementing DI by allowing the framework to manage the dependencies and inject them automatically, rather than the developer manually injecting the dependencies.

Here’s an example of IoC in action:

Suppose you have a class called “UserController” that needs to use the UserService to create a new user. With IoC, you can define the dependencies of the UserController in a configuration file, and the framework will automatically inject the UserService when the UserController is instantiated.

Here’s an example of how you might implement IoC in Spring Boot

@Configuration
public class AppConfig {
    
    @Bean
    public UserService userService() {
        return new UserService();
    }
    
    @Bean
    public UserController userController() {
        return new UserController(userService());
    }
}

In this example, the AppConfig class defines the UserService and UserController beans. When the UserController bean is created, the framework will automatically inject the UserService bean, allowing the UserController to use the UserService without knowing anything about how it is implemented.