[Java] รู้จัก Spring Boot ฉบับมือใหม่

          Spring Boot หน้าที่หลักๆ คือจัดเตรียมโปรเจคให้เรามี library ที่เกี่ยวข้องอย่างครบครัน และมี embedded application server ติดมาให้ เป็นประโยชน์ในการ test มากๆๆ

          Spring Initializr เหมาะสำหรับผู้เริ่มต้นโปรเจค Spring Boot เป็นเว็บไซต์ที่จะช่วยสร้างโปรเจคอย่างง่ายขึ้นมา โดยจะต้อง download มาเป็น *.zip หรือในบาง editor ก็สามารถสร้างโปรเจคในลักษณะเดียวกันได้เลย(ไม่ต้องผ่านเว็บก็ได้) ในเคสนี้ผมลองสร้างผ่านเว็บครับ เอาแบบ clean สุดๆ ไม่ได้ใส่อะไรเพิ่มเลยนะครับ


โหลดมาแล้วจะเห็นว่าเป็น maven project ตามที่ได้เลือกไว้ และนี่คือสิ่งที่ maven dependency ดูดมาให้เรา
mvn dependency:tree
[INFO] --- maven-dependency-plugin:3.0.1:tree (default-cli) @ SpringBoot2Tutorial ---
[INFO] th.in.lordgift:SpringBoot2Tutorial:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.0.0.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:5.0.4.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-aop:jar:5.0.4.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-beans:jar:5.0.4.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-expression:jar:5.0.4.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.0.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.4.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.4.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.0.0.RELEASE:test
[INFO]    +- org.springframework.boot:spring-boot-test:jar:2.0.0.RELEASE:test
[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.0.RELEASE:test
[INFO]    +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO]    |  +- net.minidev:json-smart:jar:2.3:test
[INFO]    |  |  \- net.minidev:accessors-smart:jar:1.2:test
[INFO]    |  |     \- org.ow2.asm:asm:jar:5.0.4:test
[INFO]    |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO]    +- junit:junit:jar:4.12:test
[INFO]    +- org.assertj:assertj-core:jar:3.9.1:test
[INFO]    +- org.mockito:mockito-core:jar:2.15.0:test
[INFO]    |  +- net.bytebuddy:byte-buddy:jar:1.7.10:test
[INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.7.10:test
[INFO]    |  \- org.objenesis:objenesis:jar:2.6:test
[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO]    +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO]    +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO]    +- org.springframework:spring-test:jar:5.0.4.RELEASE:test
[INFO]    \- org.xmlunit:xmlunit-core:jar:2.5.1:test

          จะว่าดีก็ดี จะว่าไม่ดีก็ได้ เพราะมันมี library พ่วงกันมาเยอะเหลือเกิน ซึ่งถ้าเรารู้ว่าตัวไหนไม่ได้ใช้หรือว่ามีการซ้ำซ้อนของ library ก็ควร exclude ออกไปนะครับ

ลองมาดูไฟล์ในโปรเจคกัน


*Application.java
          เป็น public static void main() ที่เราคุ้นเคยตั้งแต่หัด Java กันใหม่ๆ แล้ว ในที่นี้ข้างในมีแค่โค้ดสั้นๆ ที่จะทำให้ Spring Boot ทำการ  auto config, packaging และเอาแอปของเราไป run บน embedded tomcat พร้อมเล่นทันที

application.properties
          เป็นไฟล์สำคัญที่ตัว Auto Config จะมาอ่าน เราสามารถใส่ properties ต่างๆ เข้าไป เช่น context root, port, datasource หรืออื่นๆ ตามรูปแบบที่กำหนด แล้ว Spring จะจัดการให้เราเอง
server.servlet.context-path=/spring
server.servlet.port=8000
ในส่วนนี้เราสามารถเปลี่ยนไปใช้ format แบบ yml ก็ได้ โดยต้องเปลี่ยน application.properties เป็น application.yml ซึ่งเป็น format ที่อ่านได้ง่ายกว่า แต่ก็ขึ้นกับความถนัดของแต่ละคนอะนะ
Use YAML for External Properties
server:
  servlet:
    context-path: /spring
  port: 8000

          และ...มันมีแค่นี้ แค่นี้จริงๆ ที่ Spring Initializr เตรียมให้ ถึงแม้ว่าเราจะใส่ dependency เพิ่มเติม มันก็ไม่ได้เตรียมโค้ดตัวอย่างให้เราเลย มีแค่ pom.xml เท่านั้นที่มีการเปลี่ยนแปลง สำหรับโพสนี้คงจบไว้เท่านี้ โพสหน้าจะจัด Spring Project มาเป็นตัวอย่างเบื้องต้นครับ

Facebook Comment

Recent Posts

Popular post of 7 days

มาตรฐานการตั้งชื่อตัวแปร (Naming Convention)

Portal คืออะไรกันนะ???

[Java] ความแตกต่างระหว่าง Overloading กับ Overriding

Popular

[Java] Java 8 DateTime ใหม่ ไฉไลกว่าเดิม

Portal คืออะไรกันนะ???

มาตรฐานการตั้งชื่อตัวแปร (Naming Convention)

[Java] ความแตกต่างระหว่าง Overloading กับ Overriding

เรื่องของ ++i กับ i++

ลืมรหัสปลดล็อค Android เข้าเครื่องไม่ได้ มีทางออกครับ

[Java] Java Static Variable และ Static Method

Push Notification คืออะไร มีวิธีทำอย่างไร (No Code)

พิมพ์ซองจากรายชื่อ excel ด้วยวิธี Mail Merge

[Android] เปิดเครื่องไม่ได้ โลโก้ค้าง (Boot Loop)