Understanding Spring Core: Dependency Injection

Understanding Spring Core: Dependency Injection

Introduction

Spring Core is the foundation of the Spring Framework, and one of its most powerful features is Dependency Injection (DI). This article will help you understand what DI is, why it is important, and how you can implement it in Spring Core with the help of some pictorial diagrams.

What is Dependency Injection?

Dependency Injection is a design pattern used to implement IoC (Inversion of Control), allowing the creation of dependent objects outside a class and providing those objects to a class in various ways. It helps in creating loosely coupled applications, making them easier to test and maintain.

Types of Dependency Injection

1. Constructor Injection

Constructor Injection is a type of DI where the dependencies are provided through a class constructor.

public class Car {
    private Engine engine;

    // Constructor Injection
// the engine is being injected into car via constructor
    public Car(Engine engine) {
        this.engine = engine;
    }
}
        

2. Setter Injection

Setter Injection is a type of DI where the dependencies are provided through setter methods.

public class Car {
    private Engine engine;

    // Setter Injection
// setting engine object via setter
    public void setEngine(Engine engine) {
        this.engine = engine;
    }
}
        

3. Field Injection

Field Injection is a type of DI where the dependencies are directly injected into the fields using annotations.

class Car {
    @Autowired
    private Engine engine; // the field is engine
}
        

Implementing Dependency Injection in Spring Core

Step 1: Define the Beans

Create the classes that will act as beans. For example, let's create a Car class and an Engine class.

public class Engine {
    public void start() {
        System.out.println("Engine started.");
    }
}

public class Car {
    private Engine engine;

    public Car(Engine engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
        System.out.println("Car is driving.");
    }
}
        

Step 2: Configure the Beans in Spring

Create a configuration file (XML or Java-based) to define the beans and their dependencies.

XML Configuration

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="https://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="engine" class="com.example.Engine"/>
    <bean id="car" class="com.example.Car">
        <constructor-arg ref="engine"/> // constructor injection
    </bean>
</beans>
        

Java Configuration

@Configuration
public class AppConfig {

    @Bean // it will create engine object
    public Engine engine() {
        return new Engine();
    }

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

Step 3: Retrieve the Beans from Spring Container

Use the application context to get the beans and use them in your application.

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Car car = context.getBean(Car.class);
        car.drive();
    }
}
        

Conclusion

Dependency Injection is a core feature of the Spring Framework that promotes loose coupling and enhances testability. By understanding and implementing DI, you can build more robust and maintainable applications.

#SpringFramework #DependencyInjection #SpringCore #JavaDevelopment #IoC #SoftwareEngineering #SpringBoot #JavaProgramming #SpringDI #TechTutorials

Ratnesh Kumar Singh

Software Engineer @ Sagitec | Backend | Low-Level Design | Data Structures & Algorithms

7 个月

Thanks for sharing

Naveen Reddy

Software Engineer-Capgemini

7 个月

comment ur doubts

要查看或添加评论,请登录

Naveen Reddy的更多文章

社区洞察

其他会员也浏览了