Spring Dependency Injection

Snehatiwari
3 min readJun 24, 2021

So I am here with my new topic which is something about spring.Basically Dependency Injection topic is not so difficult but sometimes I also felt that as a beginner it can confuse us little bit so, In this blog I will try to give my best to explain you.

Spring : Spring is java framework which is the most popular framework.Actually we learn framework because it makes our application easier. We can also call Spring framework of frameworks because it can work with different framework .The aim of the Spring framework is to reduce the complexity and helps to make a lot of things like programs simpler to develop application.

Whenever we say spring it means a lot of stuff. Because Spring contains lot of modules in it just like Spring boot, Spring bean,Data integration ,Security,Session and Cloud etc.

“Spring is a dependency injection framework to make java application loosley coupled. The developer of Spring Framework is Rod Johnson in 2003.”

make java application loosely coupled means make it flexible so that you can modify application easily.

Dependency Injection: firstly we will understand what is dependency?lets take a real life example “a child is dependent on their parents because they fulfill his all needs exactly in programming or to make an application there are lot of classes which can depend to each other.”

so when the classes are depend to each other so if we change in any class or if any error occur one of the classes so its effect on both classes so we have to modified both classes and sometimes to make changes are so complex.

“so the terms Dependency Injection comes which give the functionality that to pass the object of dependent class to other class or also pass the information at the run time using external source file such as XML file we store the information of that object.It makes code loosely coupled and easier.”

we can perform dependency injection using to approach

1.By constructor

2. By Setter

so let see one example for better understand the concept:

this is employee class which conatain constuctor-

Employee.java class

this is the xml file which conatin the information of parameter that passed in employee class by using constructor approach under the bean

application.xml file

and this is the Main class which call the constructor

Main.java

I hope this article will be useful for you ..

Thank You

Sneha Tiwari

--

--