springdependenciescontrolscode-injectioninversion

What is Dependency Injection and Inversion of Control in Spring Framework?


"Dependency Injection" and "Inversion of Control" are often mentioned as the primary advantages of using the Spring framework for developing Web frameworks

Could anyone explain what it is in very simple terms with an example if possible?


Solution

  • For example: Suppose we have an object Employee and it has a dependency on object Address. We would define a bean corresponding to Employee that will define its dependency on object Address.

    When Spring tries to create an Employee object, it will see that Employee has a dependency on Address, so it will first create the Address object (dependent object) and then inject it into the Employee object.