วันอาทิตย์ที่ 13 เมษายน พ.ศ. 2557

คึกอยากจะทบทวน Spring Framework part 5

: เตรียมลุย Spring กับ XML Setter Method

จาก part 4 และก่อนหน้านี้เราคงทราบกันแล้วว่า interface มีความสำคัญอย่างไร และในโอกาสนี้เราจะเริ่มใช้งานไฟล์ XML เพื่อเข้าถึง constructor และ set methods แทนการ new object โดยตรง

เตรียม project ให้พร้อม...

ผมเลือกใช้ IDE ที่มีชื่อว่า Spring Tool Suite (3.4.0.RELEASE) เพราะต่อไปนี้เราจะต้องอาศัยความสามารถและคุณสมบัติต่างๆของมัน (เพื่ออำนวยความสะดวกไงล่ะ) ติดตั้งให้เรียบร้อยนะ

เริ่มต้นโดยเปิด project เดิมของ part 4 (spring_sample) จากนั้น copy และ paste ให้ได้อีก project หนึ่งโดยเปลี่ยนชื่อเป็น spring_sample_xml จากนั้น close เจ้า spring_sample ไปก่อน (มันรกหูรกตา)

โหลด library ของ spring framework (เวอร์ชัน 3.2.3.RELEASE) ได้จาก
http://goo.gl/szdN1P
ซึ่งประกอบด้วย
- Sources
- Javadocs
- และ Binaries

คลาย .zip ที่ได้แล้วเปิดไปที่ [เครื่องใครเครื่องมัน]\spring-framework-3.2.3.RELEASE-dist\spring-framework-3.2.3.RELEASE\libs จะเห็น .jar เต็มไปหมด

กลับมาที่ project มองหา folder ชื่อ libs (ไม่มีให้สร้างขึ้นมา) ให้เราเลือกและ copy เจ้า library สกุล .jar (ที่คลาย .zip มากี้อะ) เข้าไปใน folder ชื่อ libs ของ project ได้แก่
- spring-beans-xxx.jar
- spring-context-xxx.jar
- spring-core-xxx.jar
- และ spring-expression-xxx.jar

โหลด library ของ Apache Commons มาด้วยจาก
http://commons.apache.org/proper/commons-logging/download_logging.cgi
มองหา commons-logging-xxx-bin.zip ในหมวดของ Binaries

คลาย .zip ที่ได้แล้วเปิดไปที่ [เครื่องใครเครื่องมัน]\commons-logging-1.1.3-bin\commons-logging-1.1.3
มองหา commons-logging-xxx.jar

กลับมาที่ project มองหา folder ชื่อ libs เดิม แล้ว copy เจ้า commons-logging-xxx.jar เข้าไปรวมกับ .jar ทั้งสี่ไฟล์ก่อนหน้านี้ (ส่วนใครสงสัยว่าแต่ละ .jar ใช้ทำอะไรบ้าง ให้ลองค้นหาเพิ่มเติมเอาเองนะก๊าบ)

สุดท้ายของการเตรียมการคือเลือก .jar ทั้งห้าไฟล์แล้วคลิกขวาเลือก Build Path ต่อด้วย Add Build Path
ผลคือได้ไฟล์กระปุกแยมมาแทน ซึ่งจะอยู่ภายใต้ Referenced Libraries ทั้งหมด

เริ่มต้นสร้าง .xml ไฟล์แรก

- คลิกขวาที่ project เลือก New ต่อด้วย Spring Bean Configuration File
- ตั้งชื่อ applicationContext.xml ในช่อง File name และเลือกที่อยู่ของมันเป็น spring_sample_xml/src แล้วจึงกดปุ่ม Finish

มันจะสร้างไฟล์ applicationContext.xml ให้ เปิดไปจะพบกับโค้ด xml ดังด้านล่าง (ไม่ต้องสนใจ แค่บอกให้รู้เฉยๆ)

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

A (วางไว้เผื่อขั้นตอนด้านล่าง)
B (วางไว้เผื่อขั้นตอนด้านล่าง)

</beans>

ณ ตำแหน่ง A แทนด้วยการบอก xml ให้รู้จักกับคลาส HibernateCustomerRepositoryImpl ซึ่งจัดว่าเป็น repository เพื่อเรียกใช้เมธอด findAll เราจึงเขียน config (บางทีเรียกว่าการลงทะเบียน) ดังนี้

<bean name="customerRepository" class="com.sample.repository.HibernateCustomerRepositoryImpl" />

โดย name คือชื่อ (อะไรก็ได้) ขณะนี้ตั้งเป็น customerRepository
ส่วน class คือแหล่งอ้างอิงตาม package เพื่อระบุยังคลาส HibernateCustomerRepositoryImpl

ณ ตำแหน่ง B เมื่อ xml รู้จักกับ repository แล้ว ก็มาถึงคราวของ service ต้องลงทะเบียนบ้าง (ถ้าสงสัยว่า repository สัมพันธ์อย่างไรกับ service ให้ย้อนไปอ่าน part ก่อนหน้านี้นะ) เขียน config ได้ดังนี้

 <bean name="customerService" class="com.sample.service.CustomerServiceImpl">
 <property name="customerRepository" ref="customerRepository" ></property>
 </bean>

ให้สังเกตุ property name กับ property ref ให้ดีๆนะครับ (ลอง copy วางใน Notepad กล่าวคือ
- name หมายถึง set method ซึ่งยังไม่ได้เขียนขึ้น (เดี๋ยวเขียน)
- ref หมายถึง ชื่อที่อ้างไปยัง name ของ repository ข้างต้น ซึ่งก็คือ customerRepository

ทีนี้ service ก็จะรู้จักกับ repository แล้วจ๊ะ ขาดก็แค่ set method ของคลาส CustomerServiceImpl เท่านั้น ดังนั้นเปิดไฟล์ CustomerServiceImpl.java

- ยกเลิกการ new HibernateCustomerRepositoryImpl เพราะ xml ทำให้แล้ว จากนั้นเพิ่ม set method เข้าไปแทน ได้ดังนี้

package com.sample.service;
import java.util.List;
import com.sample.model.Customer;
import com.sample.repository.CustomerRepository;
import com.sample.repository.HibernateCustomerRepositoryImpl;

public class CustomerServiceImpl implements CustomerService {

     /*private CustomerRepository customerRepository = new HibernateCustomerRepositoryImpl();*/
     private CustomerRepository customerRepository;

     public void setCustomerRepository(CustomerRepository customerRepository) {
          this.customerRepository = customerRepository;
     }

     public List<Customer> findAll() {
          return customerRepository.findAll();
     }
}

เพียงเท่านี้เจ้าไฟล์ applicationContext.xml ก็จะไม่ error แล้วล่ะ

กลับไปแก้ไขการเรียกใช้ setCustomerRepository ในไฟล์ main เพื่อจะได้ run ดูผลลัพธ์ (รันตอนนี้เจอ null error แน่นอน) เปิดไฟล์ Application.java

- ยกเลิกการ new CustomerServiceImpl โดยตรง แต่จะ new ผ่าน .xml

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sample.service.CustomerService;

public class Application {
     public static void main(String[] args) {
          /*CustomerService service = new CustomerServiceImpl();*/
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

          CustomerService service = applicationContext.getBean("customerService", CustomerService.class);
          System.out.println(service.findAll().get(0).getFirstName());
     }
}

ผลการ run ก็จะได้
... mework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4aad08: defining beans [customerRepository,customerService]; root of factory hierarchy
Phai

ดูตรงท้ายได้ Phai ออกมาก็เป็นอันว่าเรียบร้อยครับ ซึ่งต่อไปเราจะมา config ไฟล์ xml กับ constructor กันบ้าง (เยอะไปแฮะ)

ส่วนนี่คือไฟล์ที่ทำเสร็จแล้ว แนะนำให้เปิดกับ Spring Tool Suite นะครับ
http://www.mediafire.com/download/0xv59z65g65g7s1/spring_sample_xml.zip

ไม่มีความคิดเห็น:

แสดงความคิดเห็น