Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why Dependency Injection or Service Locator?

User_19BPUJul 4 2012 — edited Jul 6 2012
Hi,

Difference between a Depency Injection(DI) and doing an interface based operation as mentioned below :-

IA ia= new A()
ia.getData();

Here I am directly using an interface IA to instantiate A() which implements IA. This is what normally we use before the use
of the DI or Servicelocator.Using the DI will ensure that you will create a setter or interface dependency for your interface
and make use of it.Apart from that DI does have an advantage when you use any mock framework as these mock frameworks are based on DI,
is there any other advantage? Why IA ia= new A() is not used often? as we are handling the dependency based on the interface reference rather than instantiating
any concreate class. Hence it should be used more often right? Please clarify.

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 3 2012
Added on Jul 4 2012
7 comments
265 views