Inversion of Control (IoC) in few simple words
What does IoC means?
Let to say that we make some meeting in some hotel.
Many people, many carafes of water, many plastic cups.
When somebody want to drink, she fill cup, drink and throw cup on the floor.
After hour or something we have a floor covered of plastic cups and water.
Let invert control.
The same meeting in the same place, but instead of plastic cups we have a waiter with one glass cup (Singleton)
and she all of time offers to guests drinking.
When somebody want to drink, she get from waiter glass, drink and return it back to waiter.
Leaving aside the question of the hygienic, last form of drinking process control is much more effective and
economic.
And this is exactly what the Spring (another IoC container, for example:
Guice ) does. Instead of let to application create what it need using new keyword (taking
plastic cup), Spring IoC container all of time offer to application the same instance (singleton) of needed object
(glass of water).
Think about yourself as organizer of such meeting. You need the way to message to hotel administration that
meeting members will need glass of water but not piece of cake.
Look at very very simple example of Spring configuration:
This way you done. How to use it from programming code? Very simple:
public class MeetingMember {
private GlassOfWater glassOfWater;
...
public void setGlassOfWater(GlassOfWater glassOfWater){
this.glassOfWater = glassOfWater;
}
//your glassOfWater object initialized and ready to use...
//spring IoC called setGlassOfWater method itself in order to
//offer to meetingMember glassOfWater instance
}
No related posts.
























[...] i/o etc.). 2. HTTP basics. 3. HTML, CSS and JavaScript basics. 4. JSP and Servlets. 5. Spring MVC, Spring IoC and Hibernate. 6. Persistence code generation and Domain Driven Frameworks. Share and [...]
[...] This post was Twitted by javaghost [...]