This document discusses dependency injection using the Spring framework. It provides an example to illustrate how dependency injection decouples classes and removes hard dependencies. Specifically, it shows how a drawing application's dependency on specific shape classes like circle and triangle can be removed by using a shape interface and dependency injection. The drawing class has its shape dependency injected from an external class rather than directly instantiating shape objects itself. This makes the drawing class more flexible and removes the need to modify it when different shape types are required.
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Learn Spring Framework and Dependency Injection in 40 Steps
1. In this tutorial we will cover spring framework and we will learn how to use spring frame work.
What is spring framework: -It’s actually a lot of things put into one.Spring is really popular for dependency
injection.
Dependency Injection: - this is sometimes called as dependency called as dependency inversion, and
definitionis “how you will decouple conventional relationship between objects that are related to each
other”i.e. one is dependent on other.
The idea is to decouple the dependencies so that they are not tied to each other. I will take an example to
explain what it means.
Example: - I have a drawing application that draws different shapes like “Circle”, “triangle”, “Square” etc..
1. I have a circle classit has a“draw ()” method.
2. And I have a triangle class that also has a method “draw ()”.
3. Now I can use these objects circle or a triangleto draw a shape onthe screen.
4. Let’s assume I have an application class.
a. I can instantiate circle objects inside this class and I can also instantiate triangle object
inside this class.
b. So code would be something like this
c. Now let’s say I don’t want my application to be tied specifically to these objects and I want
to use polymorphism.
d. Now the question is how will I use polymorphism here?
The whole Idea of using polymorphism is to have some type of interface class or some
parent class.
The example of using polymorphism is here
1. Let’s say I have a circle object and I have a triangle object both have similar draw method
2. Now I will have a “Shape” interface or a “Shape” parent class that has a draw method.
3. Now I will inherit both “Circle” and “Triangle” class from “Shape” Interface
2. 4. Now In my application class I will write code something like this
5. Here I am calling “shape.draw()”If shape has a triangle it will call draw method from
triangle
6. So this is the polymorphism here, we are calling polymorphism, instead of calling method
of the objects, I have a handle of parent class and I will call the “draw” method of parent
class. The parent could be an abstract class or it could be an interface.
7. I don’t know which object I have when I am calling “shape.draw()”
8. But here I am still tying “circle” and “triangle” objects inside “application” class.
9. So application class always know whether it is a triangle or a circle.
10. So will go one step further and I will use “Method parameter”
11. In this code you can see that somewhere in class we are calling class “new Triangle ()”.
12. Somewhere in the class we have to call “myDrawMethod(shape)“ and pass shape object
13. So we are still tied to class name, we are not free from in it.
We will take a step further
3. We will move creation of object outside the class and class will not know the shape its
drawing, the class will just have a shape, then we will expectsome other class to provide the shape for
us.
We want to have shape object instead of triangle object inside the class.
We will tell another class/object to add this triangle class to application.
1. We will create a drawing class, this class will have class member variable called shape, it
will be member variable of type shape, assume I will have a code like this –
2. I will have a private shape object.
3. And then I will define a setter “public setShape (shape)” and I will take a shape as
parameter.
4. Shape can accept anything like “circle”, “triangle”, “square” etc…
5. Whatever shape object is passed it will set the variable Shape (Member variable
Shape)inside the class.
6. Now I have a “drawShape” method this will call draw()method of class Shape note that
this does doesn’t do initialization. (you don’t see a new Shape here)
7. The drawing is gonna assume that something will instantiate that and will provide that
object to this class.
8. Now we have removed the dependency of triangle or a shape, in order to draw a triangle
you don’t have to modify the Shape class. All you need to do is pass a shape type(i.e
Triangle or circle.. etc.) to setter setShapeand then call drawShapeit will draw the desired
shape.
Example
9. The whole idea is you don’t have to modify the drawing class.
10. You are separating the dependency out from the class, the drawing class doesn’t really
now that on what it is dependent, the advantage is that “if what it has to draw is changes
you don’t have to modify the drawing class because it doesn’t really know in first place”
11. You can make drawing class to draw all kinds of shape without modifying the base class.
12. Reason – the dependency ofDrawing class to a shape object is not owned by the Drawing
class. The Drawing class doesn’t own the relationship of the actual shape.
13. The dependency of drawing class is injected by the desired shape class. Dependency of
Drawing class is not hard coded but it is injected by an external entity.
This is all about dependency Injection