เขียน Spring Application ภาคต่อจาก maven

January 14th, 2009 by roofimon Leave a reply »

เริ่มต้นเขียน application แรกด้วยสปริง
หลังจากที่บทความที่แล้วเราเริ่มสร้างโปรเจคด้วย Maven เรียบร้อยต่อไปเราจะทำการ เขียนแอพพลิเคชั่นละครับ โดยเราจะเริ่มการทำงานด้วยการเขียน Test ก่อนเพราะเราจะทำงานทั้งหมด
ด้วยแนวคิด Test First แต่ก่อนที่จะเริ่มเขียน Test เราจะต้องเข้าใจแนวคิดพื้นฐานของ Spring Container ก่อนนิดหน่อย
Spring ไม่มอะไรยากครับ ให้จำไว้เสมอว่าเราจะไม่สร้าง เซอร์วิสเองถ้าไม่จำเป็น เราจะถามเอาจาก ApplicationContext ให้สร้างให้เราเสมอ เอ่าแล้ว ApplicationContext คืออะไรเอาง่ายๆสั้นแล้วกัน
ApplicationContext ก็คือ Space หรือ Memory ที่ถูกจองไว้เพื่อใช้เก็บเซอร์วิสที่เราประกาศไว้และเมื่อใดก็ตามที่เราต้องการใช้งานเราเราเพียงแค่ใส่ชื่อเซอร์วิสที่เราต้องการเข้าไปจากนั้นจะมีคนจัด
การโยน instance ของเซอร์วิสนั้นๆกลับมาให้
ดังนั้นก่อนการทำงานทุกๆครั้ง ApplicationContext จะต้องถูกสร้าง จากนั้นมันจะทำการโหลด Configuration File ที่เราประกาศเซอร์วิสของเราไว้เข้าไปเพื่อรอให้เรามาเรียกใช้ และเพื่อไม่ให้เสียเวลา
เราลองมาสร้าง Configuration File ที่ว่านั่นกันโดยจะขอเริ่มจากเซอร์วิสง่ายๆก่อนละกัน โดยเราจะเริ่มจากการสร้างเซอร์วิสปัญญาอ่อนชื่อ “helloWorld”
แต่ช้าก่อนเราจะไม่ทำอะไรเลยถ้าเราไม่ได้เขียน test ก่อนเหอๆๆๆๆๆ กังนั้นมาประสาทแดกกันด้วยการเขียน test ก่อน โดยเราหวังว่าเราจะมี ApplicationContext ที่โหลดไฟล์ชื่อ applicationContext.xml
เข้ามาเตรียมรอท่าเราไว้ ไม่รอท่าเช่นกันไปสร้างคลาส StupidApplicationContextTest เลยครับที่ {project_name}\src\test\java ให้มีรายละเอียดดังนี้ครับ

package com.spring66.tutorial;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
/**
 *
 * @author TwinP
 */
public class StupidApplicationContextTest extends AbstractDependencyInjectionSpringContextTests{
    protected final Log log = LogFactory.getLog(getClass());
    @Override
    protected String[] getConfigLocations() {
               setAutowireMode(AUTOWIRE_BY_NAME);
        return new String[] {
                "classpath:/applicationContext.xml",
                "classpath*:/applicationContext.xml", // for modular projects
                "classpath:**/applicationContext*.xml" // for web projects
            };
    }
    public void testLoadBean(){
        HelloWorld helloService = (HelloWorld)super.applicationContext.getBean("helloWorld");
        log.debug("Frucking");
        assertNotNull(helloService);
    }
}

แดงเถือกแน่นอนอับดับแรก HelloWorld ไม่มีช้าอยู่ทำไมไปสร้างคลาส HelloWorld เลยครับที่ {project_name}\src\main\java

package com.spring66.tutorial;
/**
 *
 * @author TwinP
 */
public class HelloWorld {
    private String message;
    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }
    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }
}

ซ่อมได้ไปหนึ่งครับต่อไปไม่แดงแล้วลอง run test ดูครับว่าผ่านไหม (ผ่านคงผีหลอกครับ ==”) ไปที่ console โดยไปที่ root ของโปรเจคจากนั้นเรียก

mvn test -Dtest=StupidApplicationContextTest

ผลที่ออกมาคือ run 1 test, error 1 test ครับโดยสาเหตุที่ error สามารถดูได้ที่ {project_name}\target\surefire-reports\ ครับโดยมันจะด่าเราว่า

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: บ่น ๆๆๆๆๆๆๆ

เนื่องจากเราพยายามโหลด

        return new String[] {
                "classpath:/applicationContext.xml",
                "classpath*:/applicationContext.xml" // for modular projects
                //"classpath:**/applicationContext*.xml" // for web projects
            };

แล้วมันหาไม่เจอก๋เลยด่าเรา ==” พระเอกไม่รอช้าแก้ปัญหาอย่างเร็วพลันโดยให้เราสร้างไฟล์ชื่อ applicationContext.xml ไว้ที่ {project_name}\src\main\resources และให้เติมรายละเอียดเหล่านี้เข้าไป

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<bean id="helloWorld" class="com.spring66.tutorial.HelloWorld">
		<property name="message" value="How are you?" />
	</bean>
</beans>

หลายคนคงพอจะเดาออกถึงความหมายของเซอร์วิสปัญญาอ่อนอันนี้ bean id=”???” เป็นการบอกชื่อของเซอร์วิสที่เราต้องการใช้ class=”???” เป็นชื่อคลาสแบบเต็มยศ
นั่นคือระบุชื่อแพกเกจเข้าไปด้วยนะครับ property mane=”???” เป็นการกำหนดค่าให้กับ attribute ของคลาส ณ จังหวะที่มันจะถูกเรียกใช้งาน
ลองอีกที

mvn test -Dtest=StupidApplicationContextTest

ผลที่ได้คือ
——————————————————————————-
Test set: com.spring66.tutorial.StupidApplicationContextTest
——————————————————————————-
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.125 sec
หล่อครับ พอแค่นี้ก่อนหมดพลัง

Advertisement

2 comments

  1. Sand says:

    ขอบคุณครับผม ทำให้ spring ในความคิดของผมง่ายขึ้นมาทันทีเลย

  2. TaoHandmade says:

    สุดยอดเลยครับเพ่ เพิ่งเข้าใจปัญหาอย่างถ่องแท้

Leave a Reply

You must be logged in to post a comment.