Cover Image for Spring Dependency Injection
179 views

Spring Dependency Injection

Dependency Injection (DI) is a fundamental concept in the Spring Framework that promotes the principle of Inversion of Control (IoC). DI is a design pattern that addresses the concern of how components or objects obtain their dependencies, rather than having them created or instantiated directly within the component. In the context of Spring, DI is used to manage the relationships and dependencies between Spring beans (objects) in a Spring application.

Here’s how Dependency Injection works in Spring:

  1. Bean Definitions:
  • In a Spring application, you define beans using configuration metadata, which can be in the form of XML configuration files, Java-based configuration classes, or a combination of both.
  • Bean definitions specify the class or type of the bean, its properties, and its dependencies.
  1. Dependency Declaration:
  • When defining a Spring bean, you declare its dependencies either by specifying constructor arguments or by using setter methods (setter injection).
   <!-- Constructor Injection Example -->
   <bean id="userService" class="com.example.UserService">
       <constructor-arg ref="userRepository" />
   </bean>

   <!-- Setter Injection Example -->
   <bean id="userRepository" class="com.example.UserRepository" />
  1. Injection Mechanism:
  • Spring’s IoC container is responsible for managing bean instantiation and injecting dependencies.
  • When a bean is created, the container identifies its dependencies based on the configuration and provides those dependencies, either through constructor parameters or by calling setter methods.
  1. Wiring Dependencies:
  • The process of connecting beans with their dependencies is often referred to as “wiring.”
  • Spring’s IoC container handles the wiring process, ensuring that beans have their required dependencies available at runtime.
   public class UserService {
       private final UserRepository userRepository;

       public UserService(UserRepository userRepository) {
           this.userRepository = userRepository;
       }

       // ...
   }
  1. Advantages of Dependency Injection:
  • Decoupling: Dependency Injection promotes loose coupling between components, as each component only depends on abstractions (interfaces) and not concrete implementations.
  • Testability: It makes components easier to test in isolation, as you can provide mock or stub implementations for dependencies during testing.
  • Flexibility: You can change the behavior of a Spring application by configuring different beans without altering the application’s source code.
  • Reusability: Components become more reusable since they can be injected into different parts of the application.
  1. Types of Dependency Injection:
  • Constructor Injection: Dependencies are injected via the constructor of a class. This is often considered a good practice when dependencies are mandatory.
   public class UserService {
       private final UserRepository userRepository;

       public UserService(UserRepository userRepository) {
           this.userRepository = userRepository;
       }
   }
  • Setter Injection: Dependencies are injected via setter methods. This allows for optional dependencies or changing dependencies at runtime.
   public class UserService {
       private UserRepository userRepository;

       public void setUserRepository(UserRepository userRepository) {
           this.userRepository = userRepository;
       }
   }
  • Field Injection (not recommended): Dependencies are injected directly into fields using annotations like @Autowired. While convenient, it can lead to tight coupling.
   public class UserService {
       @Autowired
       private UserRepository userRepository;
   }

Dependency Injection is a central concept in Spring, and it plays a crucial role in achieving modularity, maintainability, and testability in Spring applications. It allows you to build complex systems by composing smaller, reusable components and managing their dependencies declaratively through configuration.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS