Dependency Injection in Spring Framework

 πŸ”§ What Is Dependency Injection?

Dependency Injection means giving an object its dependencies from the outside, rather than creating them itself.


Instead of:


java

Copy

Edit

Service service = new Service();

Controller controller = new Controller(service);

Spring does:


java

Copy

Edit

@Controller

public class MyController {

    private final Service service;


    @Autowired

    public MyController(Service service) {

        this.service = service;

    }

}

🧱 Types of Dependency Injection in Spring

1. Constructor Injection ✅ (Recommended)

java

Copy

Edit

@Component

public class Car {

    private final Engine engine;


    @Autowired

    public Car(Engine engine) {

        this.engine = engine;

    }

}

Benefits:


Encourages immutability


Easier to test


Fail-fast (required dependencies are enforced)


2. Setter Injection

java

Copy

Edit

@Component

public class Car {

    private Engine engine;


    @Autowired

    public void setEngine(Engine engine) {

        this.engine = engine;

    }

}

When to use: Optional dependencies or for mutable properties.


3. Field Injection ⚠️ (Not Recommended for Production)

java

Copy

Edit

@Component

public class Car {

    @Autowired

    private Engine engine;

}

Downsides:


Harder to test


Breaks encapsulation


Less explicit


🧠 Spring Configuration Options

✅ Using Annotations (Modern & Common)

@Component, @Service, @Repository, @Controller → mark beans


@Autowired → inject dependencies


@Qualifier → resolve conflicts when multiple beans exist


java

Copy

Edit

@Component

public class DieselEngine implements Engine {}


@Component

public class PetrolEngine implements Engine {}


@Component

public class Car {

    @Autowired

    @Qualifier("dieselEngine")

    private Engine engine;

}

πŸ“ Using XML Configuration (Legacy Style)

xml

Copy

Edit

<beans>

    <bean id="engine" class="com.example.EngineImpl"/>

    <bean id="car" class="com.example.Car">

        <constructor-arg ref="engine"/>

    </bean>

</beans>

Then load it in Java:


java

Copy

Edit

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Car car = context.getBean(Car.class);

✅ Best Practices

Use constructor injection by default.


Avoid field injection in production.


Use @Qualifier or custom annotations for multiple beans.


Combine with Spring Boot’s @SpringBootApplication and @ComponentScan to auto-discover beans.

Learn Full Stack JAVA Course in Hyderabad

Read More

Creating REST APIs using Spring Boot

Spring Boot vs Spring MVC – Key Differences

What is Spring Framework? An Overview

Java Backend Architecture – MVC Explained

Visit Our IHUB Talent Training Institute in Hyderabad

Get Directions 

Comments

Popular posts from this blog

How to Install and Set Up Selenium in Python (Step-by-Step)

Tosca for API Testing: A Step-by-Step Tutorial

Feeling Stuck in Manual Testing? Here’s Why You Should Learn Automation Testing