tags:

views:

94

answers:

4

I want to know how spring does dependency injection. I want the low level logic used.

Updates:

I want to know how the object references are injected to the constructors or setter methods, is it through Reflection or some byte code level.

Updates:

i got the answer elsewhere , the answer is bytecode instrumentation.

A: 

The PicoContainer project has some good documentation: http://www.picocontainer.org/injection.html

troelskn
+2  A: 

Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.

A class A has a dependency to class B if class uses class B as a variable.

If dependency injection is used then the class B is given to class A via

the constructor of the class A - this is then called construction injection

a setter - this is then called setter injection

The general concept between dependency injection is called Inversion of Control. A class >should not configure itself but should be configured from outside.

A design based on independent classes / components increases the re-usability and >possibility to test the software. For example if a class A expects a Dao (Data Access >object) for receiving the data from a database you can easily create another test object >which mocks the database connection and inject this object into A to test A without having >an actual database connection.

A software design based on dependency injection is possible with standard Java.

Spring just add some simplifications in using dependency injection by providing a standard >way of providing the configuration and by managing the reference to the created objects.

For more read this

Edit1:

When spring initializes its context it creates all the beans defined eager in spring application context.xml file
Now suppose your Bean A has dependency of B then the Obj of B is already with Spring as it has been created successfully while initializing it will search for setter method in class A and will set B's Obj there.

Edit2:

Please read 3.3.1.2. Setter Injection

org.life.java
Why -1 ??? ????
org.life.java
i am asking "how you are injecting in constructor"
Suresh S
@Suresh S edited
org.life.java
@org.life.java thanks , you are 90% there, but how do you " search for setter method in class A " at runtime.
Suresh S
We have already configured it in application context.xml file so it will look for class's property and for that it will look for standard setters.
org.life.java
@org.life.java please say it technically,how it looks for standard setters.
Suresh S
edited.8 more to go.
org.life.java
+1  A: 

Configuration of dependencies are read from XML, annotations or Java DSL (JavaConfig). Then Spring DI engine wires the dependencies based on the metadata from the configuration using the Java reflection API.

Henryk Konsek
@henryk, i want to know ,how it wires the dependancy.
Suresh S
Using the Java reflection API. I added that information to the answers.
Henryk Konsek
@henryk it is not using reflection , but byte code instrumentation.
Suresh S