dependency injection
DESCRIPTION
Dependency Injection is a software design pattern that allows removing hard coded dependencies and making it possible to change them at run-time.TRANSCRIPT
![Page 1: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/1.jpg)
Dependency InjectionWith Spring
Presenter: Pranav Kumar, Mindfire Solutions
![Page 2: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/2.jpg)
Agenda
What is Dependency Injection ?
Dependency Inversion Principle
Types of Dependency Injection
Why DI ?
Understand DI with StudentSystem Example
DI with Spring Configuration
Benefits of DI
DI Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
![Page 3: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/3.jpg)
DI: What it is ?
Dependency Injection is a software design pattern that allows removing hard coded dependencies and making it possible to change them at run-time.
Dependencies are injected into objects by a container.
Also Known as Inversion Of Control (IoC).
Presenter: Pranav Kumar, Mindfire Solutions
![Page 4: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/4.jpg)
DI Principle
The dependency inversion principle was postulated by Robert C. Martin
The principle states:
• Always use abstractions (Interface)
• Objects should define their dependencies only through
Constructor arguments
Arguments to a factory method.
Properties which are set on the object instance
Presenter: Pranav Kumar, Mindfire Solutions
![Page 5: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/5.jpg)
DI: Types
Setter injection - Dependent module exposes a setter method that the framework uses to inject the dependency.
Constructor injection - Dependencies are provided through the class constructor.
Presenter: Pranav Kumar, Mindfire Solutions
![Page 6: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/6.jpg)
DI: Why ?
Dependency Injection enables 'POJO programming'
POJO programming facilitate- Simplicity- Effective separation of concerns- Proper unit testing
Well-factored and well-tested code- Tends to be well-designed code- Evolves well into maintainable systems
Presenter: Pranav Kumar, Mindfire Solutions
![Page 7: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/7.jpg)
Example: The Student System
To improve your skills in Java development, you decide to develop a student system.
You decide to use a file to store student information.
You create a class FileStudentDAO responsible for writing and reading to the file
You create a class StudentSystem responsible for performing the logic of the system.
You’ve learned that it’s a good thing to program to interfaces
Presenter: Pranav Kumar, Mindfire Solutions
![Page 8: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/8.jpg)
DI:
Presenter: Pranav Kumar, Mindfire Solutions
![Page 9: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/9.jpg)
The DefaultStudentSystem
Presenter: Pranav Kumar, Mindfire Solutions
![Page 10: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/10.jpg)
Works! Or .. ?
The system is a big success – University of Oslo wants to adopt it!
Presenter: Pranav Kumar, Mindfire Solutions
![Page 11: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/11.jpg)
Works! Or .. ?
You make a new implementation of the StudentDAO for University of Oslo, a MySQLStudentDAO:
Presenter: Pranav Kumar, Mindfire Solutions
![Page 12: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/12.jpg)
Works! Or .. ?
Won’t work! The FileStudentDAO is hard-coded into the StudentSystem:
The DefaultStudentSystem implementation is responsible for obtaining a StudentDAO.
Dependent both on the StudentDAO interface and the implementation
Presenter: Pranav Kumar, Mindfire Solutions
public class DefaultStudentSystem implements StudentSystem {
private StudentDAO studentDAO = new FileStudentDAO();...
![Page 13: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/13.jpg)
Problems: Student System
How to deploy the StudentSystem at different locations?
Develop various versions for each location?
• Time consuming
• Confusing and error-prone
• Requires more efforts for versioning
Solution:
Use Dependency Injection!
• More specific term derived from the term Inversion of Control
Presenter: Pranav Kumar, Mindfire Solutions
![Page 14: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/14.jpg)
Traditional Vs DI
Presenter: Pranav Kumar, Mindfire Solutions
![Page 15: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/15.jpg)
DI: With Spring
Bean: A class that is managed by a Spring IoC container
Setter based DI: Provide a public set-method for the dependency reference.
Presenter: Pranav Kumar, Mindfire Solutions
![Page 16: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/16.jpg)
DI: Spring Configuration
Configuration: How to instantiate, configure, and assemble the objects in your application
• The Spring container accepts many configuration formats
• XML based configuration most common.
Presenter: Pranav Kumar, Mindfire Solutions
![Page 17: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/17.jpg)
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
studentDAOInjected intostudentSystem
Refers to thestudentDAO bean
Refers to thestudentDAO attribute in Java class
![Page 18: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/18.jpg)
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
![Page 19: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/19.jpg)
Summary
Presenter: Pranav Kumar, Mindfire Solutions
![Page 20: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/20.jpg)
DI: Benefits
Presenter: Pranav Kumar, Mindfire Solutions
Easier to swap implementations of dependencies
The system can be re-configured without changing the compiled code
Easier testing
Improved re-usability of your classes
Cleaner code
![Page 21: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/21.jpg)
DI: Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
Annotation is a meta-tag – applied to classes, methods...
• Associated with program behaviour
@Component
• Defines a class as a Spring container managed component
@Autowired
• Tells Spring to inject a component
Classpath scanning for components
• <context:component-scan base-package="org.example"/>
![Page 22: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/22.jpg)
DI: Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
@Componentpublic class StudentService { @Autowired StudentDAO studentDAO;
//constructors //Getter setter methods}
@Componentpublic class StudentDAO { @Override public String toString() { return "This is StudentDAO"; } }
StudentService.java StudentDAO.java
![Page 23: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/23.jpg)
DI: Using Annotations
<bean id="studentDAO" class="com.mindfire.spring.student.dao.StudentDAO"/>
<!-- Injecting studentDAO to StudentService via xml configuration -->
<bean id="studentService" class="com.mindfire.spring.student.service.StudentService">
<property name="studentDAO" ref="studentDAO" /> <!-- Setter injection -->
<constructor-arg ref="studentDAO"> </constructor-arg> <!-- Constructor injection -->
</bean>
<!-- This is used to auto scanning of beans annotated by @Component. --><context:component-scan base-package="com.mindfire.spring.student" />
Presenter: Pranav Kumar, Mindfire Solutions
Annotation configuration
XML Configuration
![Page 24: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/24.jpg)
Resources
Presenter: Pranav Kumar, Mindfire Solutions
Books
• Rod Johnson, Juergen Hoeller: Expert One-on-One J2EE
• Craig Walls and Ryan Breidenbach: Spring in Action
The Spring reference documentation
• www.springframework.org
![Page 25: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/25.jpg)
Questions ?
Presenter: Pranav Kumar, Mindfire Solutions
![Page 26: Dependency injection](https://reader033.vdocuments.net/reader033/viewer/2022052618/554f8d0bb4c9052a518b51e6/html5/thumbnails/26.jpg)
Presenter: Pranav Kumar, Mindfire Solutions