EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Dependency Injection in Spring

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Spring Tutorial » Dependency Injection in Spring

Dependency Injection in Spring

Introduction to Dependency Injection in Spring

Dependency Injection (DI) is a design pattern that implements the Inversion of Control for resolving the dependencies at run-time i.e., injecting the dependency object to the dependent object to use it at run time. The most important module in the Spring Framework is Core Container & Dependency Injection (DI) acts as the heart of Spring’s Core Container.

In a conventional way, Developers will have control over the code in creating the objects & injecting them at run time. Here, the Spring framework takes the control of doing the above-mentioned activities at run time, that’s why the term is coined as ‘Inversion of Control’ (IoC) i.e., the control is inverted!

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Perform Dependency Injection in Spring?

Before we investigate the ways of performing Dependency Injection, it is important to know about the different types of Dependency Injection Configurations. There are two different ways of Dependency Injection Configurations.

  • XML based Configuration
  • Java Annotation-based Configurations

In this article, we will focus on the Java Annotation-based Configuration, which is the most advanced, easy to implement and used widely across the software industry. Also, it is important to know about the below definition before deep diving into implementing Dependency Injection.

  • Beans
  • Autowiring

In Spring Terminology, Objects which act as the backbone of your application and are managed i.e., instantiation, configuration & assembled by the IoC Containers are referred to as Beans.

Spring Framework helps us in providing a way to detect the relationship between the beans either by reading the XML Configuration file or by scanning the Java annotations at the time of booting up the application. This task is taken up by the IoC Container – Bean Factory which will create the objects & wire the dependencies. Since Spring Framework does this process automatically, it is referred to as Autowiring i.e., Automatic Wiring!

Popular Course in this category
Spring Framework Training (4 Courses, 6 Projects)4 Online Courses | 6 Hands-on Project | 38+ Hours | Verifiable Certificate of Completion | Lifetime Validity
4.5 (5,590 ratings)
Course Price

View Course

Related Courses
Spring Boot Training Program (2 Courses, 3 Project)All in One Data Science Bundle (360+ Courses, 50+ projects)

With Java Annotation Configurations, beans are wired by using @Autowired annotation.

Different ways of Dependency Injection

As per Java Annotation Configuration, Dependency Injection can be performed in three different ways. They are as follows.

  • Constructor based Dependency Injection
  • Setter based Dependency Injection
  • Field or Property-based Dependency Injection

Let us see one by one in detail with real-time example & code snippets.

1. Constructor Based Dependency Injection

When the annotation @Autowired is used on top of the class constructor, it is referred to as Constructor based Dependency Injection. Let us witness the usage of Constructor based Dependency Injection with a real-time example.

POJO Class: EmployeeMasterDetails class

Dependency Injection - Constructor Based 1

Controller: CompanyMasterDetails class

Dependency Injection - Dependency Injection - Constructor Based 2

EmployeeMasterDetails class has been created as a Component using @Component (line 16). Spring will automatically recognize this during initialization/booting up of the application & will create a bean object using Bean Factory for EmployeeMasterDetails class.

After this, when Spring detects that EmployeeMasterDetails bean object has been Autowired (using @Autowired) into CompanyMasterDetails class constructor (line 18), the bean object created for EmployeeMasterDetails will be injected into CompanyMasterDetails class via constructor.

In this way, the Spring Framework will perform the Dependency Injection automatically. So, if you notice here, the developer has not created an object by using the “new” keyword in CompanyMasterDetails class, thereby removing the tight coupling between the CompanyMasterDetails class & EmployeeMasterDetails object.

2. Setter Based Dependency Injection

When the annotation @Autowired is used on top of the class’s setter method, it is referred to as Setter based Dependency Injection. Let us witness the usage of Setter based Dependency Injection with a real-time example.

POJO Class: EmployeeMasterDetails class

Setter Based - 1

Controller: CompanyMasterDetails class

Setter Based - 2

Similar to Constructor based Dependency Injection, here the Autowiring takes place by means of setter method at line no.18. The bean object created for EmployeeMasterDetails class will be Autowired & injected via the setter method in CompanyMasterDetails class.

3. Field or Property-Based Dependency Injection

When the annotation @Autowired is used on top of the field or property in the class, it is referred to as Field-based Dependency Injection. Let us witness the usage of Field-based Dependency Injection with a real-time example.

POJO Class: EmployeeMasterDetails class

Property Based -1

Controller: CompanyMasterDetails class

Property Based -2

Similar to Constructor based Dependency Injection, here the Autowiring takes place by means of field ‘empMasterDetails’ at line no.16. The bean object created for EmployeeMasterDetails class will be Autowired & injected via the field, ‘empMasterDetails’ in CompanyMasterDetails class.

Fall back or Error Handling cases

Suppose if there is more than one bean that is of the same type, the Spring Framework will get confused to inject the appropriate dependency. At this point, we will be running into the NoUniqueBeanDefinitionException.

This should be handled by making use of @Primary or @Qualifier annotations along with @Autowired annotation.

  • @Primary annotation: Gives priority to the other beans of the same type.
  • @Qualifier annotation: Differentiates between the other beans by providing a unique qualifier name.

Advantages of Dependency Injection

Some of the advantages of Spring’s Dependency Injection are as follows.

  • Primarily Dependency Injection helps in achieving loosely coupled architecture by removing the tight coupling/dependency between a class & its dependency.
  • As the dependency between objects is loosely coupled, it helps the developers to test the module by injecting the dependent Mock Objects (for example, making use of Spring Mockito).
  • Dependency Injection removes unnecessary dependencies between the classes.
  • As the modules are independent of each other & can be injected, the scope of making the component reusable is very high.
  • Maintenance of the System becomes easy.

Disadvantages of Dependency Injection

Some of the disadvantages of Spring’s Dependency Injection are as follows.

  • Dependency Injection makes it difficult to trace the code as the developer needs to refer to more files (such as XML Configurations) to understand how the system behaves.
  • As the Spring Framework takes care of the control rather than the developer, it will be difficult for the developers to understand how things work in the background & also to have customization.
  • As Dependency Injection allows loose coupling, it ends up increasing the number of Interfaces & classes.

Conclusion

So, in a nutshell, Dependency Injection helps the developers to achieve loose couplings between the classes. From this article, we have covered in depth of Dependency Injection, its advantages & disadvantages & different ways of performing dependency injection with real-time examples & code snippets.

Recommended Articles

This is a guide to Dependency Injection in Spring. Here we discuss the introduction, How to perform dependency injection in spring along with advantages and disadvantages. You can also go through our other suggested articles to learn more –

  1. Spring Boot Versions
  2. Spring Boot Application
  3. Spring Cloud Components
  4. Spring Architecture

Spring Framework Training (4 Courses, 6 Projects)

4 Online Courses

6 Hands-on Project

38+ Hours

Verifiable Certificate of Completion

Lifetime Validity

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
OpenCV circle

JUnit version

OpenCV haar Cascade

cPanel Alternative

jQuery equal height

Haskell Uses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Spring Framework Training (4 Courses, 6 Projects) Learn More