Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | mindfire-solutions |
View: | 818 times |
Download: | 4 times |
Dependency InjectionWith Spring
Presenter: Pranav Kumar, Mindfire Solutions
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
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
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
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
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
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
DI:
Presenter: Pranav Kumar, Mindfire Solutions
The DefaultStudentSystem
Presenter: Pranav Kumar, Mindfire Solutions
Works! Or .. ?
The system is a big success – University of Oslo wants to adopt it!
Presenter: Pranav Kumar, Mindfire Solutions
Works! Or .. ?
You make a new implementation of the StudentDAO for University of Oslo, a MySQLStudentDAO:
Presenter: Pranav Kumar, Mindfire Solutions
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();...
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
Traditional Vs DI
Presenter: Pranav Kumar, Mindfire Solutions
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
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
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
studentDAOInjected intostudentSystem
Refers to thestudentDAO bean
Refers to thestudentDAO attribute in Java class
DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
Summary
Presenter: Pranav Kumar, Mindfire Solutions
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
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"/>
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
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
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
Questions ?
Presenter: Pranav Kumar, Mindfire Solutions
Presenter: Pranav Kumar, Mindfire Solutions