Wednesday 2 March 2016

What is the difference between dependency injection and dependency look up?


  • Dependency lookup:
    • If resource of the application spends time to search and get dependent values from other resources of application, then it is called dependency lookup
    • Example :
      • If the student get his dependent value material from instruction by asking for it then is called dependency lookup.
      • The way servlet / ejb component gets jdbc data source object form registry through jndi lookup operation is called dependency lookup.
    • In dependency lookup, resource perform “pull” operation on other resource or on underlying s/w to get the dependent values.


  • <jee:jndi-lookup id="sdatasource" jndi-name="jdbc/synergy-emp-ds"/>

//@Repository ,@Component,@Controller
@Repository("LoginDaoImpl")
@Transactional
public class LoginDaoImpl implements LoginDao {
@Autowired
@Qualifier("sdatasource")
private DataSource dataSource;

}


  • Dependency injection:
When we create any application there are many classes in it which depend on each other to execute some  business logic. In that case any object which needs other objects to do its work, is responsible for getting  those dependencies itself (by using new keyword) by that way those classes are tightly coupled. 

In case of dependency injection objects  won't be creating their dependencies themselves but the dependencies will be injected into the objects. How Spring helps with dependency injection is you can define your beans in a XML file (there are other ways too) and spring will initialize the classes and create the associations. 

1 comment: