68

Possible Duplicate:
What is dependency injection?

Spring is the framework from where the concept Dependency Injection came to picture.

What is purpose of DI ? How does it benefit ? How is it implemented ?

Community
  • 1
  • 1
  • 2
    The simplest and clearest explanation of DI in code: http://codeflex.co/java-dependency-injection-simple-example/ – ybonda Mar 05 '18 at 09:28

6 Answers6

72

Start here.

Also see A-beginners-guide-to-Dependency-Injection. (Obsolete)

Elsewhere on SO:

Freak
  • 6,786
  • 5
  • 36
  • 54
Zaki
  • 6,997
  • 6
  • 37
  • 53
  • 2
    Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies in to the constructor or via property setters, and you make it somebody else's problem (an object further up the dependency graph, or a dependency injector that builds the dependency graph). A dependency as I'm using it here is any other object the current object needs to hold a reference to. – Umar Abbas Apr 03 '14 at 09:52
  • 1
    Another very helpful link I found when reading dependency injection for the first time: https://www.objc.io/issues/11-android/dependency-injection-in-java/ – Optimus Prime Nov 23 '16 at 07:17
24

What is the purpose of DI?

The purpose of Dependency Injection is to reduce coupling in your application to make it more flexible and easier to test.

How does it benefit?

Objects don't have hard coded dependencies. If you need to change the implementation of a dependency, all you have to do is Inject a different type of Object.

How does it implemented?

There are various methods of Dependency Injection. Check out the Wikipedia article to see examples of each. Once you understand those, you can start investigating the various Dependency Injection frameworks.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 2
    Hi, could you please tell me: why is it hard to test when a class depends on another class? Been reading a lot on this, I still don't get why, let say, class A uses `new` to declare class B would make test difficult. Thanks very much in advance. – Unheilig May 18 '14 at 12:34
  • @Unheilig - Testing class `A` would be difficult because you have no way to specify the behavior of `B` for your test. Remember, you only want to test your code...not all the code that your code depends upon. – Justin Niessner May 19 '14 at 14:37
13

Very short,

What is the purpose of DI? With dependency injection, objects don't define their dependencies themselves, the dependencies are injected to them as needed.

How does it benefit ? The objects don't need to know where and how to get their dependencies, which results in loose coupling between objects, which makes them a lot easier to test.

How is it implemented ? Usually a container manages the lifecycle of objects and their dependencies based on a configuration file or annotations.

nkr1pt
  • 4,691
  • 5
  • 35
  • 55
  • 2
    Hi, could you please tell me: why is it hard to test when a class depends on another class? Been reading a lot on this, I still don't get why let say class A uses `new` to declare class B would make test difficult. Thanks very much in advance. – Unheilig May 18 '14 at 12:33
  • @Unheilig https://www.youtube.com/watch?v=hBVJbzAagfs – basickarl Sep 12 '16 at 15:39
6

Try taking a look at: http://martinfowler.com/articles/injection.html

pdbartlett
  • 1,521
  • 10
  • 18
1

DI allows us to swap out components, improve testability and ensure that components are loosely coupled. DI allows to resolve dependencies at run time using DI containers such as Windsor Castle, Unity, Spring.net, MEF which allows applications to be extensible.

TrustyCoder
  • 4,749
  • 10
  • 66
  • 119
0

http://en.wikipedia.org/wiki/Dependency_injection

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79