How does dependency injection work in Spring?

15,528

Solution 1

Configuration of dependencies are read from XML, annotations or Java DSL (JavaConfig). Then Spring DI engine wires the dependencies based on the metadata from the configuration using the Java reflection API.

Solution 2

Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.

Class A has a dependency to class B if class A uses class B as a variable.

If dependency injection is used then the class B is given to class A via the constructor of the class A - this is then called construction injection; or via a setter - this is then called setter injection

The general concept of dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.

A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a Dao (Data Access Object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.

A software design based on dependency injection is possible with standard Java.

Spring just adds some simplifications in using dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.

For more read this

Edit1:

When Spring initializes its context it creates all the beans defined eager in Spring application context.xml file. Now suppose your Bean A has dependency of B then the Obj of B is already with Spring as it has been created successfully while Spring initialization. Then Spring will search for setter method in class A and will set B's Obj there.

Edit2:

Please read 5.4.1 Setter Injection

Share:
15,528

Related videos on Youtube

Dead Programmer
Author by

Dead Programmer

Sr.Java Programmer, SCEA When you torture data too much, it will reveal itself.

Updated on May 15, 2020

Comments

  • Dead Programmer
    Dead Programmer over 3 years

    I want to know how spring does dependency injection. I want the low level logic used.

    Updates:

    I want to know how the object references are injected to the constructors or setter methods, is it through Reflection or some byte code level.

    • Preet Sangha
      Preet Sangha about 13 years
      you can read the code as it's open source.
    • Dead Programmer
      Dead Programmer about 13 years
      @Preet anyway i am reading it. i want just an overview.
    • Henryk Konsek
      Henryk Konsek about 13 years
      "i got the answer elsewhere , the answer is bytecode instrumentation." - No, it's not. Spring in 95% uses Reflection API. Byte Code instrumentation is used only for specialized kind of injection like method lookup injection.
    • Dead Programmer
      Dead Programmer about 13 years
      @henry you are right byte instrumentation is for spring AOP.
    • Sam2016
      Sam2016 almost 6 years
      This is the favorite question of interviewers these days
  • Dead Programmer
    Dead Programmer about 13 years
    i want to know ,how it wires the dependancy.
  • Dead Programmer
    Dead Programmer about 13 years
    i am asking "how you are injecting in constructor"
  • Dead Programmer
    Dead Programmer about 13 years
    @org.life.java thanks , you are 90% there, but how do you " search for setter method in class A " at runtime.
  • Henryk Konsek
    Henryk Konsek about 13 years
    Using the Java reflection API. I added that information to the answers.
  • jmj
    jmj about 13 years
    We have already configured it in application context.xml file so it will look for class's property and for that it will look for standard setters.
  • Dead Programmer
    Dead Programmer about 13 years
    @org.life.java please say it technically,how it looks for standard setters.
  • flash
    flash over 10 years
    Using Java reflection API, spring will understand that it has to insert a dependency at that particular point, but how does this dependency exactly inserted? it is some bytecode level change?
  • Henryk Konsek
    Henryk Konsek over 10 years
    The actual injection is performed via reflection API as well - Method setFooMethod = class.getMethod("setFoo", Foo.class); setFooMethod.invoke(newObject, fooToInject);
  • Parasu
    Parasu almost 8 years
    @DeadProgrammer Have you got an answer to your own question "how you are injecting in constructor" ?
  • Amanuel Nega
    Amanuel Nega about 7 years
    I've seen proxy mentioned a couple of times here and there, if it is done through reflection, why do we need the proxy which requires an interface
  • Sariq Shaikh
    Sariq Shaikh almost 5 years
    So this is what my understanding is, Spring loads any class with Class.forName(...) While loading the new object dependencies are passed through constructor argument or once loaded dependencies are injected using reflection API. For special cases such as @Transactional it uses proxy method to wrap main object in proxy object. Similarly for AOP it uses Run time weaving using byte code manipulation to weave it in proxy object.