Some of the references used for this notes are (youtube - JavaBrains, Caveofprogramming, etc)
To understand Spring framework in Java, We first need to understand the flow of
Dependency Injection.
Lets consider a class
Triangle with overridden Draw() method in it that
Extends class Shape, which has its own Draw() method.
Another class, Circle with overridden Draw() method in it that
Extends class Shape as well.
Now imagine a class which needs to call the Draw() method of different subclasses(Triangle / Circle) at different stages, as below:
Class Application
{
Shape shape = new Triangle();
shape.Draw(); // this calls the Draw() in the Triangle class
Shape shape = new Circle();
shape.Draw(); // this calls the Draw() in the Circle class
}
This makes the class Application have the Shape instantiation everytime it need to call the respective Draw(), Hence making it dependent on what subclass is being passed. We could instead inject the dependency as and when needed by creating another class which has no need to know of what subclass is being passed.
Now imagine a class Drawing accepts any subclass of class Shape with no knowledge on what the subclass is that is being passed or is dependent on, as below:
Class Drawing
{
private Shape shape;
public setShape(Shape shape)
{
this.shape =shape;
}
public drawShape()
{
this.shape.draw();
}
}
Now the above class gets the passed parameter from a different class as below code:
Drawing drawing = new Drawing();
Triangle triangle =new Triangle();
drawing.setShape(triangle);
drawing.drawShape();
Now, The advantage of using Spring comes in when the different class and the Drawing class are all handled by the Spring framework. In Spring, the different class needn't even be wrtitten. How?? Move on to the next page("Setting up Spring").
To understand Spring framework in Java, We first need to understand the flow of
Dependency Injection.
Lets consider a class
Triangle with overridden Draw() method in it that
Extends class Shape, which has its own Draw() method.
Another class, Circle with overridden Draw() method in it that
Extends class Shape as well.
Now imagine a class which needs to call the Draw() method of different subclasses(Triangle / Circle) at different stages, as below:
Class Application
{
Shape shape = new Triangle();
shape.Draw(); // this calls the Draw() in the Triangle class
Shape shape = new Circle();
shape.Draw(); // this calls the Draw() in the Circle class
}
This makes the class Application have the Shape instantiation everytime it need to call the respective Draw(), Hence making it dependent on what subclass is being passed. We could instead inject the dependency as and when needed by creating another class which has no need to know of what subclass is being passed.
Now imagine a class Drawing accepts any subclass of class Shape with no knowledge on what the subclass is that is being passed or is dependent on, as below:
Class Drawing
{
private Shape shape;
public setShape(Shape shape)
{
this.shape =shape;
}
public drawShape()
{
this.shape.draw();
}
}
Now the above class gets the passed parameter from a different class as below code:
Drawing drawing = new Drawing();
Triangle triangle =new Triangle();
drawing.setShape(triangle);
drawing.drawShape();
Now, The advantage of using Spring comes in when the different class and the Drawing class are all handled by the Spring framework. In Spring, the different class needn't even be wrtitten. How?? Move on to the next page("Setting up Spring").
No comments:
Post a Comment