What is Dependency Injection?
Inversion of Control
Types of Dependency Injection
Benefits of Dependency Injection
Implement Dependency Injection Using Spring Boot
www.edureka.co
www.edureka.co
Class ClassA uses methods of B
Class A is Dependent on B
www.edureka.co
Class ClassBefore A uses methods of B
Class A needs to create an instance of class B
www.edureka.co
The process of creating an object for some other class and let the class directly using the
dependency is called dependency injection.
What is Dependency Injection(DI)?
Class DI
I need two objects
www.edureka.co
The process of creating an object for some other class and let the class directly using the
dependency is called dependency injection.
What is Dependency Injection(DI)?
Class
DI
Create Objects
A objectA = new A()
B objectB = new B()
www.edureka.co
The process of creating an object for some other class and let the class directly using the
dependency is called dependency injection.
What is Dependency Injection(DI)?
Class DI
Returns objects to the class
www.edureka.co
Dependency Injection uses three types of classes. The injector class creates an object of
the service class. Then, the injector class injects the object to a client object.
What is Dependency Injection(DI)?
Client
Class
Injector
Class
Service
Class
www.edureka.co
www.edureka.co
Concept Behind Dependency Injection
A class should not configure its dependencies statically but should be
configured by some other class from outside.
A class should concentrate on fulfilling its responsibilities like the flow of an
application, and not on creating objects
www.edureka.co
www.edureka.co
Dependencies are
provided through a
class constructor
Injector method injects
the dependency to the
setter method exposed
by the client.
Injector uses Interface
to provide the
dependency to the client
class.
Constructor Setter Interface
www.edureka.co
www.edureka.co
Email Service
Class
Objects
1. To email address
2. From email address
3. Subject
4. Body
www.edureka.co
Email Service
Class Recreate the Class
www.edureka.co
Using Dependency Injection
Change Objects at run time
www.edureka.co
Reduction of
Boiler plate
code
Unit Testing is
made much
easier
Application can
be easily
extended
Enables an easy way
to interconnect the
components
01 02 03 04
www.edureka.co
www.edureka.co
Copyright © 2017, edureka and/or its affiliates. All rights reserved.
www.edureka.co
www.edureka.co

What is Dependency Injection in Spring Boot | Edureka

  • 2.
    What is DependencyInjection? Inversion of Control Types of Dependency Injection Benefits of Dependency Injection Implement Dependency Injection Using Spring Boot www.edureka.co
  • 3.
  • 4.
    Class ClassA usesmethods of B Class A is Dependent on B www.edureka.co
  • 5.
    Class ClassBefore Auses methods of B Class A needs to create an instance of class B www.edureka.co
  • 6.
    The process ofcreating an object for some other class and let the class directly using the dependency is called dependency injection. What is Dependency Injection(DI)? Class DI I need two objects www.edureka.co
  • 7.
    The process ofcreating an object for some other class and let the class directly using the dependency is called dependency injection. What is Dependency Injection(DI)? Class DI Create Objects A objectA = new A() B objectB = new B() www.edureka.co
  • 8.
    The process ofcreating an object for some other class and let the class directly using the dependency is called dependency injection. What is Dependency Injection(DI)? Class DI Returns objects to the class www.edureka.co
  • 9.
    Dependency Injection usesthree types of classes. The injector class creates an object of the service class. Then, the injector class injects the object to a client object. What is Dependency Injection(DI)? Client Class Injector Class Service Class www.edureka.co
  • 10.
  • 11.
    Concept Behind DependencyInjection A class should not configure its dependencies statically but should be configured by some other class from outside. A class should concentrate on fulfilling its responsibilities like the flow of an application, and not on creating objects www.edureka.co
  • 12.
  • 13.
    Dependencies are provided througha class constructor Injector method injects the dependency to the setter method exposed by the client. Injector uses Interface to provide the dependency to the client class. Constructor Setter Interface www.edureka.co
  • 14.
  • 15.
    Email Service Class Objects 1. Toemail address 2. From email address 3. Subject 4. Body www.edureka.co
  • 16.
    Email Service Class Recreatethe Class www.edureka.co
  • 17.
    Using Dependency Injection ChangeObjects at run time www.edureka.co
  • 18.
    Reduction of Boiler plate code UnitTesting is made much easier Application can be easily extended Enables an easy way to interconnect the components 01 02 03 04 www.edureka.co
  • 19.
  • 20.
    Copyright © 2017,edureka and/or its affiliates. All rights reserved. www.edureka.co
  • 21.