26
Dependency Injection With Spring Presenter: Pranav Kumar, Mindfire Solutions

Dependency injection

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Dependency injection

Dependency InjectionWith Spring

    Presenter: Pranav Kumar, Mindfire Solutions

Page 2: Dependency injection

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

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

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

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

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

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

DI:

    Presenter: Pranav Kumar, Mindfire Solutions

Page 9: Dependency injection

The DefaultStudentSystem

    Presenter: Pranav Kumar, Mindfire Solutions

Page 10: Dependency injection

Works! Or .. ?

 The system is a big success – University of Oslo wants to adopt it!

    Presenter: Pranav Kumar, Mindfire Solutions

Page 11: Dependency injection

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

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

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

Traditional Vs DI

    Presenter: Pranav Kumar, Mindfire Solutions

Page 15: Dependency injection

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

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

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

DI: Spring Configuration

    Presenter: Pranav Kumar, Mindfire Solutions

Page 19: Dependency injection

Summary

    Presenter: Pranav Kumar, Mindfire Solutions

Page 20: Dependency injection

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

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

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

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

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

Questions ?

    Presenter: Pranav Kumar, Mindfire Solutions

Page 26: Dependency injection

    Presenter: Pranav Kumar, Mindfire Solutions