S02L01 – Dependency injection – Getting started

Getting Started with Dependency Injection in Java


Table of Contents:

  1. Introduction
  2. What is Dependency Injection?
  3. Setting Up a Spring-Based Dependency Injection Project
    • Installing and Configuring Spring
    • Understanding the Project Structure
  4. Step-by-Step Example of Dependency Injection in Action
    • Creating the Car Interface and Implementations
    • Configuring Beans with Spring
    • Running the Application
  5. Conclusion

1. Introduction:

Dependency Injection (DI) is a design pattern that enables the creation of flexible and testable applications by decoupling object creation from their usage. In Java, especially when working with frameworks like Spring, DI plays a significant role in building modular and maintainable code. This guide will walk you through a practical example of setting up dependency injection using Spring, helping you to understand its importance and how to implement it in a real-world scenario.

By the end of this guide, you will be able to implement DI in a Java project, improving code readability and testability.


2. What is Dependency Injection?

Dependency Injection is a pattern in which an object receives its dependencies from external sources rather than creating them itself. In other words, an object’s dependencies are “injected” at runtime, promoting loose coupling between components.

Pros and Cons of Dependency Injection:

Pros Cons
Reduces tight coupling between objects Can introduce complexity in simple applications
Improves code modularity and testability Overuse may lead to excessive abstraction
Easier to manage dependencies in large systems Requires familiarity with DI frameworks like Spring

3. Setting Up a Spring-Based Dependency Injection Project

3.1 Installing and Configuring Spring:

Spring Framework is widely used to manage DI in Java projects. In this example, we are using a Maven-based Spring project, and the configuration is managed through the pom.xml file. You can add the Spring dependencies to your project by including the following in your pom.xml file:

3.2 Understanding the Project Structure:

Here is a breakdown of the key files in the project:

  • App.java: The main class that initializes the Spring context and demonstrates DI.
  • AppConfig.java: The configuration file that defines the beans.
  • Car.java: An interface representing a car.
  • Swift.java and Corolla.java: Concrete implementations of the Car interface.

4. Step-by-Step Example of Dependency Injection in Action

4.1 Creating the Car Interface and Implementations:

First, we define the Car interface and two concrete implementations: Swift and Corolla.

Car.java (Interface):

Swift.java:

Corolla.java:

4.2 Configuring Beans with Spring:

In Spring, we configure the DI by defining beans in a configuration class. This allows Spring to manage the lifecycle of objects and inject dependencies where needed.

AppConfig.java:

In this configuration class, we define a Car bean, which returns an instance of Corolla. Spring will handle the injection of this bean wherever required.

4.3 Running the Application:

Now, we bring everything together in the App.java class:

App.java:

This main class initializes the Spring application context, retrieves the Car bean (which is a Corolla in this case), and calls its specs() method.

The output will be:

5. Conclusion:

Dependency Injection simplifies object creation and allows for more modular, maintainable, and testable code. In this article, we explored how to set up a Spring-based Java project, define dependencies as beans, and inject them into our application. By following this guide, you will have a solid understanding of how DI works in Spring and how you can leverage it to improve your Java applications.

Make sure to explore further by adding more complex dependencies and experimenting with different configurations.