Introduction to Spring (Dependency Injection)

Image for post
Image for post

In this tutorial series I am going to cover spring framework and teach how to use spring framework in our java applications.

It is actually a lot of things put into one but the thing is that spring is really popular for dependency injection.We will learn that later from a good introduction to Spring. As the core of this tutorial we look at other capabilities of Spring and other roles that Spring perform in our application.

This is sometime called Dependency Inversion. Actually it is conventional dependency relationships between objects. Say you have two Objects related to each other (One is dependent on the other) and the idea is to decouple.They are tied to each other. Let’s take an Example to explain what is the concept means. Let’s say I have a drawing application. I have the code that draws different shapes like Triangle, Square, Circle and Other. I am using that code to draw different shapes on the screen.

I have a Circle Object — Circle object has a draw method

I have a Triangle Object — Triangle object has a draw method

Image for post
Image for post

I can use these objects in order to draw a Circle or a Triangle on the screen. Let’s say I have an application class and I can instantiate Circle object inside this application class and draw a Circle. Similarly I can instantiate Triangle object inside this application class and draw a Triangle.

Image for post
Image for post

Actually, I do not want my application class to be tight. Specifically to these objects. I can use Polymorphism. How do I use Polymorphism here?. Whole idea of Polymorphism is having an interface class or Parent class. I use the parent class in order to execute the methods of the class and then at the run time I describe different children of the parent class. And the methods of the child gets executed. Let me clarify that by showing you How I would modify the piece of code in order to implement Polymorphism.

Lets say, I have the Circle Object and Triangle object. Both have similar draw method. What I’ll do is I will have a shape interface or a shape parent class that has a draw method. I will make both the Circle object and the Triangle object inherit form the Shape interface or Parent class. Now what is going to happen is the Circle object is going to overwrite the draw method of the Shape. Triangle object does the same thing.

Image for post
Image for post

Now If I have to use Polymorphism in my application class, I can write a code like this.

Image for post
Image for post

Instead of initiating a Triangle or Circle what I’ll do is

Shape shape = new Triangle();
Shape shape = new Circle();

I will initiate a Triangle and Circle object and put those objects inside a Shape object. Now I am going to call shape.draw(). So this is simply Polymorphism in action. We are doing something that uses Polymorphism concepts instead of calling a method of the object itself. I have to handle the parent object and call the draw method. This parent object could be an Abstract class or an Interface. The whole idea is that I do not know which object I have when I am calling particular shape.draw();

Actually, this is not really Polymorphism. I am still instantiating a Circle object inside Application class. The Application class always know whether it is a Triangle or a Circle. Instead of having the code hard coded what I am going to do is creating a method called myDrawMethod.

Image for post
Image for post

This method take Shape as a parameter. This Shape could be anything(Circle or Triangle). All this method does is just call shape.draw();. If you pass a Circle object this method will draw a Circle. Actually, myDrawMethod does not worry about what shape it is. It knows only what kind of object has been passed — Shape Object. We have actually removed the dependency of the Circle or the Triangle from the myDrawMethod.

Note that somebody has to pass the object to the myDrawMethod method.There has to be another piece of code in this class which has the initialization.

Image for post
Image for post

Still we had to instantiate the object Triangle or Circle. So that we will take a one more step further. We can take Triangle or Circle dependency outside of the class. Then the class will not know what shape to draw. Class will just have a Shape. And then we will expect another class to provide the Shape for us.

Image for post
Image for post

Right Now in our Application class we have this Triangle object because we have new Triangle(); What we need to do is we need to have a Shape object instead of a Triangle Object. And we will ask somebody else (Some other class or object) to add Triangle or Circle object into Application class object.

This is how it is going to work.

I have a Drawing class and this drawing class will have a class member variable called Shape. It is not going to be a member variable of Triangle or Circle. It is a member variable of type Shape. Assume that I have a code like this.

Image for post
Image for post

I will define a public setter and it will accept a Circle, Triangle or any other Shape. What ever shape object is passed it sets as a member variable inside Drawing class. And then I have a drawShape() method and it just calls a draw method of Shape. Here somebody wants to instantiate object and provide object to to this class. Now we have totally removed the dependency from a Triangle or Shape.

If you want to draw a Triangle you do not want to modify the Drawing class. All you need to do is pass a Triangle to the setter and then do drawShape() method.

In a different class you will do these things.

Image for post
Image for post

So now the advantage here is, In our drawing instead of having a specific Shape as a Triangle or a Circle it has an instance of the parent Shape object .

Image for post
Image for post

“Different class” which has a Triangle object, pass the Triangle to Drawing class. And then Drawing class has a Triangle. It is open for new objects to be instantiate as long as the object is a Shape. You do not have to modify this Drawing class. You just have to pass the corresponding object in a Shape. Then the Drawing class will draw it. You only should change the “Different class”here.

The whole idea is that you are separating the whole dependency out of a class. Drawing class does not know what is the drawing object and we do not want to change the Drawing class. You can use the Drawing class to draw all kinds of Shapes without even changing a line of code of the Drawing class.

The dependency of a Drawing class to a Shape object is not owned by the Drawing class. Dependency that the Drawing class has(Dependency to the Triangle) actually injected to the Drawing class by something else(Injected by Different class).

So this is the principle of Dependency Injection.Here you can Inject the dependency of Drawing class to a Triangle. So the dependency is not hard-coded in the class. It is actually injected by the from outside the class.

This is something that Spring makes it very easy to code. By Spring you just tell to inject this dependency to the object. You configure Spring to inject the correct dependency to the correct object.

This is a very high level overview of Dependency Injection and I hope the concepts are clear. In the next tutorial we will use Spring to actually implement the Dependency Injection.

Thank You !!

Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Software International

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store