Posts Tagged ‘ioc’
java IOC for starters
I never thought wordpress will be kind enuff to provide syntax highlighting. So i can start posting all the source code that i use to learn new technologies here. I’m still new with this IOC kind of things, so expect only the basic usage of it in this posting. I assume u guys already knew how to setup a eclipse with the spring jar.
1) Create the test case. Because I’m more prone using TDD practices, so the test wil fail at 1st.
package spring;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
import log.SpringLog;
public class TestCharacter extends TestCase
{
private static Logger log;
private static ClassPathXmlApplicationContext applicationContext;
private ComicCharacter character;
@Override
protected void setUp() throws Exception
{
log = SpringLog.getLog();
if(applicationContext == null)
{
applicationContext = new ClassPathXmlApplicationContext("ioc.xml");
}
this.character = (ComicCharacter)applicationContext.getBean("comicCharacter");
log.debug(this.character.getName());
}
public void testAttribute()
{
assertNotNull(this.character);
assertEquals(999999, this.character.getHealthPoint());
}
public void testHikmat()
{
assertNotNull(this.character.getHikmat());
assertEquals("tinju maut pendekar laut", this.character.getHikmat().famousAttack());
}
}
This code will only not run but it also will throw a exception. Because we haven’t write any ioc.xml yet.
2) Write the ioc.xml .
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="comicCharacter" class="spring.ComicCharacter">
<property name="healthPoint">
<value>999999</value>
</property>
<property name="hikmat" ref="hikmatPenderkarLaut" />
</bean>
<bean id="hikmatPenderkarLaut" class="spring.HikmatPenderkarLaut">
<property name="famousAttack" value="tinju maut pendekar laut" />
</bean>
</beans>
The ioc.xml will tell the spring to initialize comicCharacter with its healthPoint attribute with value 999999. Then comicCharacter also have a attribute called hikmat (this is an interface actually). Using ioc, we wired this attribute to class HikmatPendekarLaut(this class implement hikmat interface).But when u run the test case, the result will also yield a fail situation(the good news no more spring exception). Because we haven’t write the concrete class for this test case.
3) Write the concreate class to make the test go green (success).
package spring;
public class ComicCharacter
{
private int healthPoint;
private String name;
private Hikmat hikmat;
public int getHealthPoint()
{
return healthPoint;
}
public void setHealthPoint(int healthPoint)
{
this.healthPoint = healthPoint;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Hikmat getHikmat()
{
return hikmat;
}
public void setHikmat(Hikmat hikmat)
{
this.hikmat = hikmat;
}
}
package spring;
public class HikmatPenderkarLaut implements Hikmat
{
private String famousAttack;
public String famousAttack()
{
return this.famousAttack;
}
public void setFamousAttack(String famousAttack)
{
this.famousAttack = famousAttack;
}
}
package spring;
public interface Hikmat
{
String famousAttack();
}
the test case should go green.
![OMG ! [ Explore #1 ] OMG ! [ Explore #1 ]](http://static.flickr.com/2631/4150305338_5dd4270c04_t.jpg)



