Overview (Dependency Injection)

di-decouple-hdr-img1
DI (Dependency Injection) is one of key features in Spring framework beside (AOP). Spring is the framework that try to decouple the program in many way.

DI is one of ways to decouple the program like a Factory Pattern. In your code, you can GET RID OFnew” statement to create an object.


Example of Dependency:
In your code, the method A need object A1 in order to run it.

Example of Dependency Injection:
Your method A doesn’t have to create a object A1, instead, the object can be injected from somewhere else.

What is good about it?
Your method A can run it without object A1 that makes easier to test it.


There are three ways to do this in Spring framework: