Dependency injection

What is dependency injection ?

Questions by retheesh   answers by retheesh

Showing Answers 1 - 11 of 11 Answers


  • Jan 11th, 2008

Dependency injection or Inversion of control are the two terms used for same thing. In case of DI, we define the process of instatiating an object into a non java class generally xml file.

Instead of using new word to instatiate any object, reference of an object is retrieved from an config file.

  Was this answer useful?  Yes

The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don't directly connect your components and services together in code but describe which services are needed by which components in a configuration file.

  Was this answer useful?  Yes


  • Jun 26th, 2008

 Objects can be injected through setter or constructor.
Dependency Injection means we can inject all the dependenciess to ur object in the xml file.

For example in order to establish connection ,Connection object needs datasource object.

So in the xml file first we declare a bean for DataSource ,in that we give parameters for connecting to database like url username passwordetc
We delare another bean named Connection in which the above declared datasource object is injected through setter or constructor.

So at the time of startup the container reads the xml file. ann all the required objects are created without the developer explicitly creating objects.

Dependency Injection is also know as IoC(Injection of control)

By using this Injectioin we can instantiates and creates the objects for the non-java class using XMl file

its bean based defination And give the instance whenever asked by the client called Dependeny Injection

Please update it, if any thing else

  Was this answer useful?  Yes

Dependency injection is the ability to inject collobrative objects dynamically.Suppose you want to call a method of particular class, so for that you use new operator help and make object of that class and call the require method using ref. varibale of that class .But when you use Spring dependency injection , you don't have to do all these stuff, instead of that you configure a xml file in which you mention what all objects you require in you class, it will get ready for use. You don't have to create or instantiate .


Rajeev Chaturvedi


  • Oct 19th, 2009

Dependency Injection means instead of an object looking up dependencies from the container the container gives the dependent object at instantiation time without waiting to be asked.

  Was this answer useful?  Yes

Here before going to Dependency injection I want mention lilttle bit about DependencyLookup l. - If the main application or object  serches for getting dependent values  require for it then it is called dependency lookup.

Example: The way you get JDBC connection obj from JDBC connection pool by serching datasource obj in naming registry  from any J2EE component  is call dependency lookup.

Dependency Injection or Inversion of Control(IOC): If the underlying framework s/w or container s/w creates the main obj, creates the dependent  values and assign them dynamically to main obj  then it is called dependency injection or inversion of Control.

Example: The way JVM creates Java class obj  & stores  initial values  to obj. by automatically ececuting  constructor comes under dependency injection.

The way struts f/w  creates formbean class obj. & stores html, JSP form data into it dynamically that obj comes under dependency injection.

  Was this answer useful?  Yes

One object has to depend on other object.i.e when i try to create one object without knowing to user internally it is going to create another object. Example: class Employee { Integer empid; String empname; }so when I try to create Employee object then it is going to create empid as Integer object and empname as String with knowing to user one object is going to create another object.

  Was this answer useful?  Yes


  • Mar 8th, 2011

Spring terms of Inversion of Control is called dependency injection.

Instead of creating dependency class B instances from Object A (A--> B), dependency injection is the design pattern suggesting the reverse. The dependent B would be injected into A, on container startup.

This is normally configured in context.xml in Spring, or using annotations in classes.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions