DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introduction to Apache Kafka With Spring
  • Dependency Injection in Spring
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Migrating Spring Java Applications to Azure App Service (Part 1: DataSources and Credentials)

Trending

  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  • Code Reviews: Building an AI-Powered GitHub Integration
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD
  • The End of “Good Enough Agile”
  1. DZone
  2. Coding
  3. Frameworks
  4. How Dependency Injection (DI) Works In Spring Java Application Development

How Dependency Injection (DI) Works In Spring Java Application Development

This article explains the concept of Dependency Injection (DI) and how it works in Spring Java application development.

By 
Ethan Millar user avatar
Ethan Millar
·
May. 30, 16 · Opinion
Likes (25)
Comment
Save
Tweet
Share
257.8K Views

Join the DZone community and get the full member experience.

Join For Free

This article explains the concept of Dependency Injection (DI) and how it works in Spring Java application development. You will learn about the advantages, disadvantages, and basics of DI with examples. Look further for more information.

Advantages of Dependency Injection

  • DI allows a client the flexibility of being configurable. Only client's behavior is fixed.

  • Testing can be performed using mock objects.

  • Loosely couple architecture.

  • DI advantages of high cohesion are:

    • Reduced module complexity

    • Increased system maintainability, because logic changes in the domain affect fewer modules.

    • Increased module reusability.

  • DI does not require any changes in code behavior it can be applied to legacy code as refactoring.

  • DI allows a client to remove all knowledge of a concrete implementation that needs to use. It is more reusable, more testable, more readable code.

  • DI makes it possible to eliminate, or at least reduce unnecessary dependencies.

  • DI allows concurrent or independent development.

  • DI decreases coupling between a class and its dependency.

Disadvantages of Dependency Injection

  • DI creates clients that demand configure details supplied by construction code.

  • DI can make code difficult to trace because it separates behavior from construction; this means developers refer to more files to follow how a system performs.

  • DI can cause an explosion of types, especially in languages that have explicit interface types like C# and Java.

  • DI can encourage dependence on DI framework.

  • Tight coupling :

    • A change in only one module usually forces a ripple effect of changes in other modules.

Dependency Injection

Dependency Injection (DI)

  • Dependency Injection (DI) is a software design pattern that implements inversion of control for resolving dependencies.

  • An injection is the passing of a dependency to a dependent object that would use it.

  • DI is a process whereby objects define their dependencies. The other objects they work with—only through constructor arguments or arguments to a factory method or property—are set on the object instance after it is constructed or returned from a factory method.

  • The container then injects those dependencies, and it creates the bean. This process is named Inversion of Control (IoC) (the bean itself controls the instantiation or location of its dependencies by using direct construction classes or a Service Locator).

  • DI refers to the process of supplying an external dependency to a software component.

Dependency Injection Performed Two Ways

1. Constructor-Based Dependency Injection

  • Constructor-based DI is when the container invokes a constructor with a number of arguments, each of which represents a dependency or other class.

  • Calling a static factory method with particular arguments to construct the bean is approximately equivalent, treating arguments to a constructor and to a static factory method. The following example shows a class that can only be dependency-injected with constructor injection. It is a POJO that has no dependencies on container specific interfaces, base classes, or annotations.

public class SimpleStudentList {
    // the SimpleStudentList has a dependency on StudentFind
    private StudentFind studentFind;
    // a constructor that Spring container can 'inject' a StudentFind
    public SimpleStudentList(StudentFind studentFind ) {
        this.studentFind = studentFind ;
    }
    // business logic code
}

Example of Constructor-Based DI

Book.java
package com.spring.example;  

public class Book {  
    private int id;  
    private String bookName;  

    public Book() {System.out.println("Java");}  

    public Book(int id) {this.id = id;}  

    public Book(String bookName) {  this.bookName = bookName;}  

    public Book(int id, String bookName) {  
        this.id = id;  
        this.bookName = bookName;  
    }  
    void display(){  
        System.out.println(id+" "+bookName);  
    }  
}  
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="book" class="com.spring.example.Book">  
<constructor-arg value="1" type="int"></constructor-arg>  
</bean>  

</beans>  
Main.java
package com.spring.example;  

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  

public class Main {  
    public static void main(String[] args) {  

        Resource r=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(r);  

        Book b=(Book)factory.getBean("book");  
        b.display();  

    }  
}  
Output:

1 null

2. Setter-Based Dependency Injection

Setter-based DI is the when the container calls setter methods on your beans after it has invoked a no-argument constructor or no-argument static factory method to instantiate that bean.

The following example shows a class that can only have pure setter injection.

public class SimpleStudentList {
    // the SimpleStudentList has a dependency on StudentFind
    private StudentFind studentFind;
    // a setter method that Spring container can 'inject' a StudentFind
    public void setStudentFind(StudentFind studentFind ) {
        this.studentFind = studentFind ;
    }
    // business logic
}

Example of Setter Based DI

Book.java
package com.spring.example;  

public class Book {  
    private int id;  
    private String bookName;  
    private String author;  

    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getBookName() {  
        return bookName;  
    }  
    public void setBookName(String bookName) {  
        this.bookName = bookName;  
    }  

    public String getAuthor() {  
        return author;  
    }  
    public void setAuthor(String author) {  
        this.author = author;  
    }  
    void display(){  
        System.out.println(id+" "+bookName+" "+author);  
    }  
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="book" class="com.spring.example.Book">  
<property name="id">  
<value>1</value>  
</property>  
<property name="bookName">  
<value>The Complete Reference J2EE</value>  
</property>  
<property name="author">  
<value>Herbert Schildt</value>  
</property>  

</bean>  

</beans>  
Main.java
package com.spring.example;  

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  

public class Main {  
    public static void main(String[] args) {  

        Resource r=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(r);  

        Book b=(Book)factory.getBean("book");  
        b.display();  

    }  
}  
Output :
  • The Complete Reference J2EE  Herbert Schildt

Our Java application development team has just explained the concept of Dependency Injection, its advantages, disadvantages, and uses in Spring with examples. If you still have any confusion, tell us and get the answer from professionals.

Dependency injection Spring Framework application Java (programming language) Inversion of control

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Apache Kafka With Spring
  • Dependency Injection in Spring
  • Enterprise RIA With Spring 3, Flex 4 and GraniteDS
  • Migrating Spring Java Applications to Azure App Service (Part 1: DataSources and Credentials)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!