Flat Preloader Icon

Dependency Injection in Spring

  • Dependency Injection (DI) is a fundamental concept in the Spring Framework, and it plays a pivotal role in managing the dependencies between components (beans) in a Spring-based application. DI is a design pattern that promotes the inversion of control, where the control over the creation and management of object dependencies is shifted from the application code to a container, such as the Spring IoC (Inversion of Control) container.
Here’s an explanation of Dependency Injection in the context of Spring:

Dependency:

  • In the context of DI, a “dependency” is an object that a class relies on to perform its functions. For example, a Car class might have a dependency on a Engine class to operate.

Injection:

  • “Injection” refers to the process of providing dependencies to a class when it is created or initialized. Instead of the class creating its own dependencies, they are “injected” into it from an external source.

Spring Bean:

  • In Spring, a Java object that is managed by the Spring IoC container is called a “bean.” Beans are typically created and configured by the container and can represent various application components, such as services, DAOs (Data Access Objects), or controllers.

Types of Injection:

  • Spring supports several types of Dependency Injection:
    • Constructor Injection: Dependencies are injected via the constructor of the class.
    • Setter Injection: Dependencies are injected through setter methods.
    • Field Injection: Dependencies are injected directly into fields (usually annotated with @Autowired or similar annotations).
    • Method Injection: Dependencies are injected into methods, often used for specific use cases.

Here's how Dependency Injection works in Spring:

Dependency Configuration:

  • You define your application’s components (beans) in a Spring configuration file (XML-based or Java-based) or through annotations. These components are configured with their dependencies.

Spring Container:

  • When your Spring application starts, the Spring IoC container reads the configuration and instantiates the beans, including their dependencies.

Injection:

  • The container injects the dependencies into the beans based on the configuration. This can be done through constructor injection, setter injection, field injection, or method injection, depending on how you’ve configured your beans.

Usage:

  • .Your application code can use these beans without worrying about how they are created or where their dependencies come from.
Here’s an example of Dependency Injection in Spring using Java-based configuration:
				
					@Configuration
public class AppConfig {

    @Bean
    public Engine engine() {
        return new Engine();
    }

    @Bean
    public Car car() {
        return new Car(engine());
        // Constructor Injection
    }
}

				
			
Dependency Injection in Spring promotes loose coupling between components, making your code more modular and easier to test. It also enables better separation of concerns and easier maintenance of your application as it grows.