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

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

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

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Smart Dependency Injection With Spring: Overview (Part 1 of 3)
  • Dependency Injection in Spring
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

Trending

  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  • Memory Leak Due to Time-Taking finalize() Method
  • How to Build Real-Time BI Systems: Architecture, Code, and Best Practices
  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Framework Basics: What Is Inversion of Control?

Spring Framework Basics: What Is Inversion of Control?

What is Inversion of Control? And how does the Spring Framework implement it?

By 
Ranga Karanam user avatar
Ranga Karanam
DZone Core CORE ·
Apr. 12, 19 · Presentation
Likes (15)
Comment
Save
Tweet
Share
45.2K Views

Join the DZone community and get the full member experience.

Join For Free

Developers starting with the Spring Framework often get confused with the terminology, specifically dependencies, dependency injection, and Inversion of Control. In this article, we introduce you to the concept of Inversion of Control.

What You Will Learn

  • What is Inversion of Control?
  • What are some examples of Inversion of Control?
  • How does the Spring Framework implement Inversion of Control?
  • Why is Inversion of Control important and what are its advantages?

What Is Inversion of Control?

Approach-1

Have a look at the following implementation of ComplexAlgorithmImpl:

public class ComplexAlgorithmImpl {
BubbleSortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm();
//...
}


One of the numerous things that ComplexAlgorithmImpl does is sorting. It creates an instance of BubbleSortAlgorithm directly within its code.

Approach-2

Now, look at this implementation for a change:

public interface SortAlgorithm {
public int[] sort(int[] numbers);
}

@Component
public class ComplexAlgorithmImpl {
@AutoWired
private SortAlgorithm sortAlgorithm;
//...
}


ComplexAlgorithmImpl here makes use of the SortAlgorithm interface. It also provides a constructor or a setter method where you can set the SortAlgorithminstance into it. The user tells ComplexAlgorithmImpl, which sort algorithm to make use of.

Comparing Approach-1 and Approach-2

Approach-1

  • ComplexAlgorithmImpl can only use BubbleSortAlgorithm; it is tightly coupled.
  • If we need to change ComplexAlgorithmImpl to use quicksort, the relevant code needs to be changed entirely.
  • The control over the BubbleSortAlgorithm dependency is with the ComplexAlgorithmImpl class.

Approach-2

  • ComplexAlgorithmImpl is open to using any implementation of SortAlgorithm, it is loosely coupled.
  • We only need to change the parameter we pass to the constructor or setter of ComplexAlgorithmImpl.
  • The control over the SortAlgorithm dependency is with the user of ComplexAlgorithmImpl.

Inversion Of Control At Play!

In Approach-1, ComplexAlgorithmImpl is tied to a specific sort algorithm.

In Approach-2, it says: give me any sort algorithm and I will work with it.

This is Inversion of Control.

Instead of creating its own dependencies, a class declares its dependencies. The control now shifts from the class to the user of the class to provide the dependency.

Why Is Inversion of Control Important?

Once you write code with Inversion of Control, you can use frameworks like Spring to complete dependency injection and wire up beans and dependencies.

Advantages of Inversion Of Control

  • Inversion of Control makes your code loosely coupled
  • Inversion of Control also makes it easy for the programmer to write effective unit tests

Lastly, be sure to check out the video below on IoC:

image info


Summary

In this article, we talked about Inversion of Control. Instead of a class creating an instance of its own dependency, it leaves it to the user of the class to pass it in and makes code loosely coupled.

Hope you learned something! Let us know what you think in comments below.

Spring Framework Inversion of control Framework Dependency injection

Published at DZone with permission of Ranga Karanam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Smart Dependency Injection With Spring: Overview (Part 1 of 3)
  • Dependency Injection in Spring
  • Graceful Shutdown: Spring Framework vs Golang Web Services
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4

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!