A JeDi Journey (there is no certainty, only opportunity)

yesterday padawan will become tommorow jedi

Posts Tagged ‘spring

here come the workload…again

without comments

Ok just a reminder to myself, I need to do all these things in this week;

  • find a way to do translation on wordpress automatically
  • build a simple web application using spring framework
  • brush up my skill with ibatis. Should be easy but i don’t want to take any chances.
  • explore joomla. Using wordpress to create a simple website is not a good idea. 

With mok wedding on saturday. I pretty sure my schedule will be pack this week. Not to mentioned nowdays I can play DOTA from home. Good since I need to save every single penny.

Written by peysal

March 26, 2009 at 2:07 pm

Posted in career, work

Tagged with , ,

java IOC for starters

without comments

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.

Written by peysal

April 2, 2008 at 3:41 am

Posted in whatEver

Tagged with ,