File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes How is IoC useful over conventional object instantiation ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Other Application Frameworks
Reply Bookmark "How is IoC useful over conventional object instantiation ?" Watch "How is IoC useful over conventional object instantiation ?" New topic
Author

How is IoC useful over conventional object instantiation ?

Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Hi All,

I'm relatively new to Spring and espicially to IoC. Unrelated to Spring, can some one explain me with an example, how IoC is useful over conventional object instantiation (using new keyword). What is the benefit of this re-direction, where in creation and wiring of the objects is delegated to IoC container instead of handling it in code. I'm pretty sure that, there might be something apart from freeing the code from object creation, which does not seem to be lot of problematic.


Thanks & Regards, SK
SCJP 5.0, DB2 - 800, DB2 - 803, SCDJWS (On the way)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12612
IoC allows us to decouple the interface from the implementation, and can allow external forces to determine the implementation.

Consider the implications for testing method foo:We can only test foo() with an instance of a Bar. If we need to test the return value of foo(), we now have to re-implement our Bar.baz() to return each of the possible conditions foo() could encounter. If, on the other hand, we allow the Bar to be set, our unit test can decide which condition we're testing:IoC also allows for strong configurability. Consider a program intended to allow easy extension or modification of key components. Instead of going in and changing the original source code, we can just tell the program at startup which implementation to use. This might allow us to customize how HTTP requests are handled, provide a plugin mechanism, and so on.
Schandha Ravi
Ranch Hand

Joined: Oct 20, 2007
Posts: 167
Thank you for explaining with an example. I have started reading "Spring in action" to understand more on this framework and I'm sure that I would come up with more questions to ask.
 
IntelliJ Java IDE
 
subject: How is IoC useful over conventional object instantiation ?
 
Threads others viewed
For the authors--What is Spring??
New book published: Spring Recipes: A Problem-Solution Approach
Inversion of Control:question
What is dependency injection?
Which technology is best to study in parallel
IntelliJ Java IDE