Sunday 28 February 2016

Dependency Lookup


The Dependency Lookup is an approach where we get the resource after demand. Various ways to get the resource are mentioned below:

Using new keyword
ClassA obj = new ClassAImpl(); 

Static factory method
ClassA  obj = ClassA.getClassA(); 

Using JNDI (Java Naming Directory Interface) :
Context ctx = new InitialContext();
Context environmentCtx = (Context) ctx.lookup ("java:comp/env"); 
ClassA obj = (ClassA)environmentCtx.lookup("ClassA "); 

Problems of Dependency Lookup
Tight coupling: The dependency lookup approach makes the code tightly coupled. If resource is changed, we need to perform a lot of modification in the code.

Not easy for testing: This approach creates a lot of problems while testing the application especially in black box testing.

Dependency Injection (DI) is a design pattern that removes the dependency from the code so that it can be easy to manage and test the application. It makes our programming code loosely coupled.

This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...