SlideShare uma empresa Scribd logo
1 de 3
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
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
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

Mais conteúdo relacionado

Semelhante a Spring tutorial - dependency injection

Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxMahmoodAlashqar
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptxHailsh
 
Alice 3
Alice 3Alice 3
Alice 3frcol
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Contoh Factory pattern
Contoh Factory patternContoh Factory pattern
Contoh Factory patternFajar Baskoro
 
Please make the complete program, distinguishing between each class .pdf
Please make the complete program, distinguishing between each class .pdfPlease make the complete program, distinguishing between each class .pdf
Please make the complete program, distinguishing between each class .pdffaxteldelhi
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan vo
 
The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189Mahmoud Samir Fayed
 
08review (1)
08review (1)08review (1)
08review (1)IIUM
 
Oop.concepts
Oop.conceptsOop.concepts
Oop.conceptstahir266
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196Mahmoud Samir Fayed
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 

Semelhante a Spring tutorial - dependency injection (20)

Abstract
AbstractAbstract
Abstract
 
Abstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptxAbstract vs Concrete Classes.pptx
Abstract vs Concrete Classes.pptx
 
Class and Object.pptx
Class and Object.pptxClass and Object.pptx
Class and Object.pptx
 
Alice 3
Alice 3Alice 3
Alice 3
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Contoh Factory pattern
Contoh Factory patternContoh Factory pattern
Contoh Factory pattern
 
Classes2
Classes2Classes2
Classes2
 
Please make the complete program, distinguishing between each class .pdf
Please make the complete program, distinguishing between each class .pdfPlease make the complete program, distinguishing between each class .pdf
Please make the complete program, distinguishing between each class .pdf
 
Reflection
ReflectionReflection
Reflection
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189The Ring programming language version 1.6 book - Part 75 of 189
The Ring programming language version 1.6 book - Part 75 of 189
 
Oop
OopOop
Oop
 
08review (1)
08review (1)08review (1)
08review (1)
 
Oop.concepts
Oop.conceptsOop.concepts
Oop.concepts
 
What is design pattern
What is design patternWhat is design pattern
What is design pattern
 
The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196The Ring programming language version 1.7 book - Part 78 of 196
The Ring programming language version 1.7 book - Part 78 of 196
 
Calc ii complete
Calc ii completeCalc ii complete
Calc ii complete
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 

Último (20)

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Spring tutorial - dependency injection

  • 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