From the previous post we know spring makes development a kids play and POJO is as awesome as its name.Second of the four strategies which make it super awesome will be looked at in the post.
Pearl 2: D.I.
Ok, so POJO was good but that is what every java class creates objects. We need more to show coolness of Spring. Now, when I say the coolness of spring is that it follows Inversion of Control Design pattern and more specifically it has dependency injection, you can accuse me of being a man of big words and no meaning. So lets first see what is DI aka dependency injection.
Dependency Injection:
So when we create an application it has usually more than one classes that collaborate with each other to do things. So now when we create object of one class(say A) which needs help of another class(say B) we create an object of B and pass its reference to object of A. This leads to hard coupling and makes it harder to test!! Ok another jargon!! Hard coupling is when two objects are stuck together and can't do things without each other and cant be like mohammed ali(that is float like a butterfly and sting like a bee). Lets take an example.
package com.perius.spring;
public class MohammedAli implements Boxer{
private Butterfly butterfly;
private Bee bee;
public MohammedAli(){
butterfly=new FastButterfly();
bee = new StingingBee();
}
public void fight()[
butterfly.float();
bee.sting();
}
}
Just for the sake of the code the boxer interface
package com.perius.spring;
public interface Boxer{
public void fight();
}
Now when we create a mohammed ali object the constructor will get called and it gets its butterfly and bee to float and sting respectively. So we see that mohammed ali has become tightly coupled to FastButterFly and StingingBee.
Now lets say the boxing coach has come up with new technique and now you have SuperFastButterFly floating technique and a KillerStingingBee stinging technique which just knocks your opponent out. Poor MohammedAli can't get any of those even when he has interfaces Bee and butterfly because he is tightly coupled to FastButterFly and StingingBee objects. And thus JOE has won it.
Basically our MohammedAli class is difficult to test and reuse and understand too and if a bug is introduced in StingingBee it will come to MohammedAli too and there ain't anything you can do about it.
So now lets look at JoeFrazier who uses dependecy injection
package com.perius.spring;
public class JoeFrazier implements Boxer{
private Butterfly butterfly;
private Bee bee;
public JoeFrazier(Butterfly butterfly,Bee bee){
this.butterfly=butterfly;
this.bee = bee;
}
public void fight()[
butterfly.float();
bee.sting();
}
}
What we have done with Joe is that we have made him loosely coupled. So when Joe gets constructed he gets the butterfly and bee injected to its object at runtime from a third party (may be by his coach :P). So he can get the latest buttterfly floating technique and the latest stinging bee technique.
This is what the spring provides a dependency injection. So when you create beans in such a way that you can give it any object it needs (Alway program to the interfaces!!!!!) spring provides way of creating these runtime associations. This is called wiring. There are many ways to create these wirings but the most ancient of them is via XML lets name it boxer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="joe" class="com.perius.spring.JoeFrazier">
<contructor-arg ref="butterfly"/>
<contructor-arg ref="bee"/>
</bean>
</bean>
<bean id="bee" class="com.perius.spring.SuperStingingBee">
</bean>
</beans>
There are a lot of other things in wiring. This is just introduction.
Now how do we use it.The application context loads the bean definition in boxer.xml and wires them together. The spring application context is fully responsible for creation and wiring of objects so you dont need to worry. There are different implementations of application context each differing in how and from where is it loading the bean definition. ClassPathXmlApplicationContext is one such implementation which searches the bean definition in the classpath of the application and loads the beans.
Ex:
package com.perius.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ClassPathXmlApplicationContext;
Voila! You have a fight!!
Pearl 2: D.I.
Ok, so POJO was good but that is what every java class creates objects. We need more to show coolness of Spring. Now, when I say the coolness of spring is that it follows Inversion of Control Design pattern and more specifically it has dependency injection, you can accuse me of being a man of big words and no meaning. So lets first see what is DI aka dependency injection.
Dependency Injection:
So when we create an application it has usually more than one classes that collaborate with each other to do things. So now when we create object of one class(say A) which needs help of another class(say B) we create an object of B and pass its reference to object of A. This leads to hard coupling and makes it harder to test!! Ok another jargon!! Hard coupling is when two objects are stuck together and can't do things without each other and cant be like mohammed ali(that is float like a butterfly and sting like a bee). Lets take an example.
package com.perius.spring;
public class MohammedAli implements Boxer{
private Butterfly butterfly;
private Bee bee;
public MohammedAli(){
butterfly=new FastButterfly();
bee = new StingingBee();
}
public void fight()[
butterfly.float();
bee.sting();
}
}
Just for the sake of the code the boxer interface
package com.perius.spring;
public interface Boxer{
public void fight();
}
Now when we create a mohammed ali object the constructor will get called and it gets its butterfly and bee to float and sting respectively. So we see that mohammed ali has become tightly coupled to FastButterFly and StingingBee.
Now lets say the boxing coach has come up with new technique and now you have SuperFastButterFly floating technique and a KillerStingingBee stinging technique which just knocks your opponent out. Poor MohammedAli can't get any of those even when he has interfaces Bee and butterfly because he is tightly coupled to FastButterFly and StingingBee objects. And thus JOE has won it.
Basically our MohammedAli class is difficult to test and reuse and understand too and if a bug is introduced in StingingBee it will come to MohammedAli too and there ain't anything you can do about it.
So now lets look at JoeFrazier who uses dependecy injection
package com.perius.spring;
public class JoeFrazier implements Boxer{
private Butterfly butterfly;
private Bee bee;
public JoeFrazier(Butterfly butterfly,Bee bee){
this.butterfly=butterfly;
this.bee = bee;
}
public void fight()[
butterfly.float();
bee.sting();
}
}
What we have done with Joe is that we have made him loosely coupled. So when Joe gets constructed he gets the butterfly and bee injected to its object at runtime from a third party (may be by his coach :P). So he can get the latest buttterfly floating technique and the latest stinging bee technique.
This is what the spring provides a dependency injection. So when you create beans in such a way that you can give it any object it needs (Alway program to the interfaces!!!!!) spring provides way of creating these runtime associations. This is called wiring. There are many ways to create these wirings but the most ancient of them is via XML lets name it boxer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean id="joe" class="com.perius.spring.JoeFrazier">
<contructor-arg ref="butterfly"/>
<contructor-arg ref="bee"/>
</bean>
<bean id="butterfly" class="com.perius.spring.SuperFloatingButterfly">
<bean id="bee" class="com.perius.spring.SuperStingingBee">
</bean>
</beans>
There are a lot of other things in wiring. This is just introduction.
Now how do we use it.The application context loads the bean definition in boxer.xml and wires them together. The spring application context is fully responsible for creation and wiring of objects so you dont need to worry. There are different implementations of application context each differing in how and from where is it loading the bean definition. ClassPathXmlApplicationContext is one such implementation which searches the bean definition in the classpath of the application and loads the beans.
Ex:
package com.perius.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ClassPathXmlApplicationContext;
public class Ring{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("boxer.xml");
Boxer boxer=(Boxer) context.getBean("joe");
joe.fight();
}