The document discusses dependency injection with Spring. It defines dependency injection as a design pattern that allows removing hardcoded dependencies and changing them at runtime. It presents different types of dependency injection like setter and constructor injection. It demonstrates how to configure dependency injection with Spring using XML configuration files or annotations and inject dependencies into objects. Benefits of dependency injection include easier testing, improved reusability, and cleaner code.
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
10. Works! Or .. ?
The system is a big success – University of Oslo wants to adopt it!
Presenter: Pranav Kumar, Mindfire Solutions
11. Works! Or .. ?
You make a new implementation of the StudentDAO for University of Oslo, a
MySQLStudentDAO:
Presenter: Pranav Kumar, Mindfire Solutions
12. 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();
...
13. 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
15. 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
16. 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
17. DI: Spring Configuration
Presenter: Pranav Kumar, Mindfire Solutions
studentDAO
Injected into
studentSystem
Refers to the
studentDAO
bean
Refers to the
studentDAO attribute in Java
class
20. 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
21. 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"/>
22. DI: Using Annotations
Presenter: Pranav Kumar, Mindfire Solutions
@Component
public class StudentService
{
@Autowired
StudentDAO studentDAO;
//constructors
//Getter setter methods
}
@Component
public class StudentDAO
{
@Override
public String toString() {
return "This is StudentDAO";
}
}
StudentService.java StudentDAO.java
23. 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
24. 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