Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
In this tutorial we will cover spring framework and we will learn how to use spring frame work.What is spring framework: -...
4. Now In my application class I will write code something like this5. Here I am calling “shape.draw()”If shape has a tria...
We will move creation of object outside the class and class will not know the shape itsdrawing, the class will just have a...
Upcoming SlideShare
Loading in …5
×

Spring tutorial - dependency injection

456 views

Published on

Published in: Software, Technology, Spiritual
  • Login to see the comments

Spring tutorial - dependency injection

  1. 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 dependencyinjection.Dependency Injection: - this is sometimes called as dependency called as dependency inversion, anddefinitionis “how you will decouple conventional relationship between objects that are related to eachother”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 toexplain 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 objectinside this class.b. So code would be something like thisc. Now let’s say I don’t want my application to be tied specifically to these objects and I wantto 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 someparent class.The example of using polymorphism is here1. Let’s say I have a circle object and I have a triangle object both have similar draw method2. 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. 2. 4. Now In my application class I will write code something like this5. Here I am calling “shape.draw()”If shape has a triangle it will call draw method fromtriangle6. So this is the polymorphism here, we are calling polymorphism, instead of calling methodof the objects, I have a handle of parent class and I will call the “draw” method of parentclass. 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 object13. So we are still tied to class name, we are not free from in it.We will take a step further
  3. 3. We will move creation of object outside the class and class will not know the shape itsdrawing, the class will just have a shape, then we will expectsome other class to provide the shape forus.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, itwill 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 asparameter.4. Shape can accept anything like “circle”, “triangle”, “square” etc…5. Whatever shape object is passed it will set the variable Shape (Member variableShape)inside the class.6. Now I have a “drawShape” method this will call draw()method of class Shape note thatthis 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 thatobject to this class.8. Now we have removed the dependency of triangle or a shape, in order to draw a triangleyou don’t have to modify the Shape class. All you need to do is pass a shape type(i.eTriangle or circle.. etc.) to setter setShapeand then call drawShapeit will draw the desiredshape.Example9. 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 reallynow that on what it is dependent, the advantage is that “if what it has to draw is changesyou 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 Drawingclass. 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 ofDrawing class is not hard coded but it is injected by an external entity.This is all about dependency Injection

×