How do DI and IoC really do?
Dependency Injection
//Hardcoded dependency public class MyClass { private MyDependency myDependency = new MyDependency(); }
This is DI design pattern
//Injected dependency public class MyClass { private MyDependency myDependency; public MyClass(MyDependency myDependency){ this.myDependency = myDependency; } }when we inject new Object to the myDependency
IoC
So here is where IoC comes to the rescue
With IoC, the dependencies are managed by the container, and the programmer is relieved of that burden.
Using annotations like @Autowired, the container is asked to inject a dependency where it is needed, and the programmers do not need to create/manage those dependencies by themselves.
public class MyClass1 { @Autowired private MyClass2 myClass2; public void doSomething(){ myClass2.doSomething(); } }
Read EasyDI to understand why IoC can create object for dependency
https://github.com/lestard/EasyDI
Comments
Post a Comment