Spring's IoC is one major feature of Spring framework. Martin Fowler has beautifully made clear and explained what IoC is about. You can find his article on Ioc here.
Spring configuration file as XML describes how to configure the classes. Who is dependent on who and how to give them what they want. As per Martin Fowler's article, instead of MovieLister picking itself the specific implementation of a MovieLister, we configure Spring to inject this MovieFinder implementation into MovieLister. Thus MovieLister can act as a plugin with no dependencies within itself.
How to inject them ? Three ways exists as mentioned in the article
1. Type 1 - Interface injection
2. Type 2 - Setter injection
3. Type 3- Constructor injection
Spring supports both type 2 and type 3 injection. Constructor injection is favoured when working with Spring and it is simple of all for those who have been regularly working with JavaBeans specification.
Dependency Injection is opposite to Service Locator. When to choose between them depends on how you want your services to be utilised. For simple services which are not used much outside the application or the specific module, then simple Service Locator will suit. But if there exists a functionality which needs to be executed in simulated mode and production mode, you can very well use dependency injection. The simualted mode will have unit test cases which can be changed over and over and provides a clean testing of the functionality without any regression coming out of touching your actual application code.
Judgement is the Key. Shouldn't get carried away. Numerous solution exists for every problem but what suits ? This requires better understanding of the application.
Lets see how spring.xml would be for the moviefinder and movielister example
Spring configuration file as XML describes how to configure the classes. Who is dependent on who and how to give them what they want. As per Martin Fowler's article, instead of MovieLister picking itself the specific implementation of a MovieLister, we configure Spring to inject this MovieFinder implementation into MovieLister. Thus MovieLister can act as a plugin with no dependencies within itself.
How to inject them ? Three ways exists as mentioned in the article
1. Type 1 - Interface injection
2. Type 2 - Setter injection
3. Type 3- Constructor injection
Spring supports both type 2 and type 3 injection. Constructor injection is favoured when working with Spring and it is simple of all for those who have been regularly working with JavaBeans specification.
Dependency Injection is opposite to Service Locator. When to choose between them depends on how you want your services to be utilised. For simple services which are not used much outside the application or the specific module, then simple Service Locator will suit. But if there exists a functionality which needs to be executed in simulated mode and production mode, you can very well use dependency injection. The simualted mode will have unit test cases which can be changed over and over and provides a clean testing of the functionality without any regression coming out of touching your actual application code.
Judgement is the Key. Shouldn't get carried away. Numerous solution exists for every problem but what suits ? This requires better understanding of the application.
Lets see how spring.xml would be for the moviefinder and movielister example

