Custom Search
Monday, September 10, 2007
Types of Dependency Injections.
1 Type 1 IOC also called Interface Injection

o In Type 1 IOC the injection is done though an interface. The interface will define the injection method and the implementation class has to implement this interface and provide concrete implementation for the injection method.

2 Type 2 IOC also called Setter Injection

o In Type 2 IOC the injection is done via a setter method. Type 2 IOC uses setter methods to get the dependent classes it needs.

3 Type 3 IOC also called Constructor Injection.

o In Type 3 IOC implementing class defines a constructor to get all its dependents. The dependent classes are defined in the constructor arguments.

Spring supports only Constructor based injection and setter based injection.

Spring recommends setter-based injection over constructor-based injection as multiple constructors can make the bean huge and unmanageable.
Saturday, September 08, 2007
How does Spring Work?
The idea is that beans follow the Dependency Injection pattern. Beans have information on the classes dependent on them. Beans define their own attributes within bean descriptors and also define the beans they depend on in the same descriptors.

 The Bean does not need to locate and instantiate these classes using service locators or JNDI calls. The Assembler (Spring Framework in this case) takes care of finding these classes based on the information provided in the descriptor and makes them available to the calling class.

 The service locator pattern does almost the same job, so why do we need another pattern to do it?
o The class that needs the dependent classes needs to tell the Service Locator as to which classes are required by it and moreover the responsibility of finding these classes and invoking them falls on the calling class. This makes the classes tightly coupled with each other making them difficult to unit test them separately.


 In the case of the Dependency Injection pattern the responsibility is shifted to the Assembler to load these classes. The assembler can make changes to the dependent classes by simply modifying the descriptor file.