Dependency injection

1,181 views

Published on

Dependency Injection is a software design pattern that allows removing  hard coded dependencies and making it possible to change them at run-time.

Published in: Technology
  • Be the first to comment

Dependency injection

  1. 1. Dependency Injection With Spring Presenter: Pranav Kumar, Mindfire Solutions
  2. 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. 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. 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. 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. 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. 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
  8. 8. DI: Presenter: Pranav Kumar, Mindfire Solutions
  9. 9. The DefaultStudentSystem Presenter: Pranav Kumar, Mindfire Solutions
  10. 10. Works! Or .. ? The system is a big success – University of Oslo wants to adopt it! Presenter: Pranav Kumar, Mindfire Solutions
  11. 11. Works! Or .. ? You make a new implementation of the StudentDAO for University of Oslo, a MySQLStudentDAO: Presenter: Pranav Kumar, Mindfire Solutions
  12. 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. 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
  14. 14. Traditional Vs DI Presenter: Pranav Kumar, Mindfire Solutions
  15. 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. 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. 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
  18. 18. DI: Spring Configuration Presenter: Pranav Kumar, Mindfire Solutions
  19. 19. Summary Presenter: Pranav Kumar, Mindfire Solutions
  20. 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. 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. 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. 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. 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
  25. 25. Questions ? Presenter: Pranav Kumar, Mindfire Solutions
  26. 26. Presenter: Pranav Kumar, Mindfire Solutions

×