Programming & Development / May 7, 2025

Dependency Injection in Java: A Complete Guide with Spring Framework

Dependency Injection Java DI Spring Framework IoC container loose coupling constructor injection field injection setter injection Spring Boot Java design patterns

Dependency Injection (DI) is a powerful design pattern in Java that promotes loose coupling, testability, and modularity by allowing objects to receive their dependencies from an external source, rather than creating them internally.

It is a core concept in frameworks like Spring, and mastering it is essential for writing scalable and maintainable Java applications.

1. What Is Dependency Injection?

Dependency Injection is a design pattern where dependencies (objects) are injected into a class, instead of the class creating them directly.

Example of tight coupling:

java

class Car {
    Engine engine = new Engine(); // tightly coupled
}

With DI:

java

class Car {
    private final Engine engine;

    public Car(Engine engine) {
        this.engine = engine; // dependency is injected
    }
}

2. Types of Dependency Injection in Java

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection (discouraged outside of frameworks)

3. Why Use DI?

  • Promotes loose coupling
  • Makes code easier to test
  • Improves flexibility and maintainability
  • Encourages use of interfaces and abstraction

4. DI in Plain Java (Manual Example)

java

class Service {
    void serve() { System.out.println("Serving..."); }
}

class Client {
    private final Service service;

    public Client(Service service) {
        this.service = service;
    }

    void doWork() {
        service.serve();
    }
}

5. Dependency Injection with Spring Framework

Spring uses an IoC (Inversion of Control) container to manage beans and inject dependencies automatically.

java

@Component
public class Engine {}

@Component
public class Car {
    private final Engine engine;

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

Spring Boot automatically scans and wires components using annotations like:

  • @Component
  • @Service
  • @Repository
  • @Autowired
  • @Configuration / @Bean

6. Constructor vs Setter Injection in Spring

Constructor Injection (Recommended):

java

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

Setter Injection (Optional or circular dependencies):

java

@Autowired
public void setEngine(Engine engine) { this.engine = engine; }

7. Field Injection (Less Preferred)

java

@Autowired
private Engine engine;

Although shorter, it's harder to test and not recommended for new code.

8. Java Configuration with @Bean

java

@Configuration
public class AppConfig {

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

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

9. Dependency Injection vs Service Locator

FeatureDependency InjectionService LocatorPush modelYesNo (pull)Encourages testingYesNot idealDecouples logicYesPartially


10. Conclusion

Dependency Injection is a fundamental pattern for building clean, testable Java applications. Frameworks like Spring make it incredibly powerful and easy to use. By mastering DI, you unlock the ability to write flexible, loosely coupled code that scales with your project.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex