Spring Tutorial 01 - Understanding Dependency Injection
Introduction to the Spring Framework
Overview of Spring Framework
- The tutorial series introduces the Spring Framework, focusing on its application in Java development.
- A key feature of Spring is dependency injection, which will be explored in this session.
Understanding Dependency Injection
- Dependency injection (also known as dependency inversion) decouples conventional dependencies between objects.
- An example is provided using a drawing application with shapes like circles and triangles to illustrate how objects can depend on one another.
Polymorphism in Action
- The speaker explains polymorphism by introducing an interface or parent class that allows different child classes (like Circle and Triangle) to override methods.
- By using a shape interface, both Circle and Triangle can implement their own draw methods while being treated as Shape types.
Implementing Polymorphism
- In the application class, instead of directly instantiating specific shape objects, a Shape reference is used for flexibility.
- This approach demonstrates polymorphism where the method called depends on the actual object type assigned to the Shape reference.
Moving Beyond Hardcoding
- The discussion highlights that simply using a Shape reference still ties code to specific shapes; thus, further abstraction is needed.
- A new method called
myDrawtakes a Shape parameter, allowing it to call draw without knowing specifics about Circle or Triangle.
Understanding Dependency Injection in Drawing Classes
The Problem of Hard-Coded Dependencies
- The speaker discusses the necessity of passing a shape object to the
myDrawmethod, emphasizing that an object (like a triangle or circle) must be instantiated within the class.
- It is highlighted that if a triangle is hard-coded into the class, any changes to draw a different shape would require modifying the class itself, indicating tight coupling.
- The need for flexibility is introduced; changing from drawing a triangle to a circle necessitates altering the code where the shape is instantiated.
Moving Towards Decoupling
- To improve flexibility, the speaker suggests taking dependencies outside of the class. The drawing class should not know which specific shape it will draw.
- A new approach involves having a generic shape object instead of specific instances like triangles or circles directly in the application class.
Implementing Dependency Injection
- Introduction of a
Drawingclass with a member variable of typeShape, allowing for any shape type to be set without hard-coding specifics.
- A public setter method (
setShape) is defined in this drawing class, enabling it to accept various shapes (e.g., circles, triangles), promoting reusability and scalability.
Benefits of Decoupled Design
- By removing direct instantiation from within the drawing class, it can now operate independently from specific shapes. This allows for easy substitution and addition of new shapes without modifying existing code.
- The drawing process becomes straightforward: regardless of whether it's instructed to draw a triangle or circle, as long as an appropriate shape object is passed through
setShape, it will function correctly.
Flexibility and Future-Proofing
- This design pattern ensures that future modifications (like adding new shapes such as pentagons or hexagons) do not require changes in the drawing logic itself.
- An example illustrates how another part of the program can instantiate shapes and pass them into the drawing class without needing knowledge about what specific shape is being drawn.
Conclusion on Dependency Management
- The overall advantage lies in separating concerns; while some classes may still need modification when introducing new types (like switching from triangles to circles), this separation minimizes changes required across multiple classes.
- Ultimately, this approach fosters cleaner architecture by ensuring that classes remain focused on their responsibilities without being tightly coupled with specific implementations.
Dependency Injection in Drawing Classes
Understanding Dependency Injection
- The drawing class can draw various shapes without modifying its code because it does not own the relationship with specific shape objects; it only requires a generic shape.
- The drawing class's dependency on a shape (like a triangle or circle) is injected from an external source, meaning the class itself does not instantiate these shapes directly.
- This principle of dependency injection allows for flexibility; you can switch out dependencies (e.g., from triangle to circle) without altering the drawing class's internal logic.
- Spring framework simplifies this process by managing the instantiation and injection of dependencies, allowing developers to focus on higher-level configurations rather than boilerplate code.
- Developers can configure Spring to automatically inject the correct dependencies into objects, streamlining development and reducing manual coding efforts.