Most of the developer worked in Java is aware of spring frame work. And who doesn't know anything about Spring framework can think about experienced spring season. Like many other frame works available Spring also serve as container. It creates the java object/component run time and manage them. The application developer can concentrate on business logic and really forget about managing objects. When people talk about Spring two terms dependency injection and inversion of controller always comes. Let me also write about these... When there is a situation a object use another object (association).
Lets have an example:
Class Orchestra
{
Dancer dancer = new Dancer();
Singer singer = new Singer();
perform()
{
dancer.dance();
singer.sing();
}
}
Here Orchestra class use Dancer and Singer to perform the job. Well tomorrow we need some modern dancing and some different kind of Song we have to change the Orchestra class.
In Spring way we can re write some thing like below.
Class Orchestra
{
IDancer dancer;
ISinger singer;
perform()
{
dancer.dance();
singer.sing();
}
}
Interface IDancer
{
dance();
}
Interface ISinger
{
sing();
}
BreakDancer implements IDancer
{
dance()
{
----
----
}
}
RockSinger implements ISinger
{
sing()
{
----
----
}
}
In Runtime we can inject the dancer and Singer to Orchestra using Spring. Well here with singer and dancer we are injecting the dependency to Orchestra. And Orchestra gave the control which kind of dancer and singer is going to perform. This is something called inversion of control. We can do both xml based or annotation based configuration in Spring. Here is the xml version
<bean id="orchestra" class="com.action.Orchestra">
<property name="singer"><ref bean="rockSinger"/></property>
<property name="dancer"><ref bean="breakDancer"/></property>
</bean>
<bean id="rockSinger">
<property name="target">
<bean class="com.action.RockSinger" />
</property>
</bean>
<bean id="breakDancer">
<property name="target">
<bean class="com.action.BreakDancer" />
</property>
</bean>
So spring gives such a environment where we have all the freedom to configure anything up to any level.
Have a nice time in Spring :)