UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Spring - Dependency Injection (DI)

Gaurav Kothari
18/11/2020 0 0

Spring - Dependency Injection (DI)

DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object from some configurations (XML based, java based, annotation-based).

DI is nothing but design patterns which give you the lightweight loosely couple enterprise application in the result.

What is an injection? -- Inserting of an object/ value into a class whenever the help requires it of third party configurations.

There is two way of achieving Dependency Injection:

  • By Constructor
  • By Setter method

Constructor Injection: Injecting any object/ value through the constructor of a class. For, e.g., I have a Test class with the parameterized constructor in it, and I am initializing parameter of that constructor from the external source, i.e. XML file. See how

public class Test { 

private int id; 

public Test(int id) {

this.id = id;

}

XML configuration file: //here now 100 will be assigned to the id of Test class

 

Setter method Injection: Injecting any object/ value through the setter method of a class. For, e.g., I have a Test class with setter method in it, and I am initializing parameter of that setter method from the external source, i.e. XML file. See how

public class Test { 

private int id; 

public int getId() { 

  return id; 

public void setId(int id) { 

   this.id = id; 

}

}

XML configuration file: //here now 100 will be assigned to the id of Test class

 

Please add on comments and like this lesson, if it gave you some revision or refreshment on Spring - Dependency Injection (DI) concepts.

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

CONDITIONAL STATEMENT - IF ELSE
1. IF condition only if is true conditon is required. if(condition){//statements} 2. IF-ELSE condition 1. to check whether the condition will be true or false.syntax of if-else2. only 1 conditionif(condition){//statements}else{//statements} 3....

Java8 Filters and collectors
Lets say we have collection of strings and we would like to filter (remove) out certain strings from collection. We could achive the same in java 7 and earlier versions import java.util.ArrayList; import...

Session Tracking In Java Servlets
Session Tracking: HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected...

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

What Is The Difference Between Scope And Lifetime?
Scope of a variable is defined as the block of code from where we can refer or access it. On the other hand the life time of a variable is defined as the time in between allocating memory for it and relinquishing...
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more