Spring Boot & Framework - Spring Boot.pdf · spring-boot-starter-jdbc...

Post on 07-Aug-2020

62 views 0 download

Transcript of Spring Boot & Framework - Spring Boot.pdf · spring-boot-starter-jdbc...

1

ธระยทธ ทองเครอ

ภาควชาวทยาการคอมพวเตอร คณะวทยาศาสตร

มหาวทยาลยขอนแกน

Spring Framework & Spring Boot

Java Framework Technologies

2

Java Runtime Platform

3

Spring FrameworkFramework ส ำหรบพฒนำแอปพลเคชนส ำหรบองคกรบนแพลตฟอรม

ของจำวำ

เปำหมำยของ Spring คอ ลดเวลำในกำรพฒนำ และเสนอแนวปฏบตทดใหกบนกพฒนำ

เปน Open source ทมขนำดเลก (Lightweight) และสำมำรถเพมขยำยไดงำย

4

Spring Framework

5

Dependency Injection Dependency Injection เรยกอกชอหนงวำ IOC (Inversion of Control Principle)

เปนวธกำรออกแบบซอฟทแวรตำม pattern ทมกำรแยก component ตำง ๆ ใหมควำมเกยวของกนนอยทสด (loosely coupled)

Component แตละสวนสำมำรถน ำมำประกอบกน และสำมำรถทดสอบไดอยำงงำยดำย ชวยให reuse code ไปใชใน application อน ๆ ไดงำย ไมมกำร Hard code หรอก ำหนดคำภำยในคลำสตำงๆ โดยตรง แตจะแยกไฟลส ำหรบ Config

ในรปแบบ XML configuration files หรอ จำวำคลำสส ำหรบ Config คำโดยเฉพำะ Spring Container คอ ผทท ำหนำทในกำรฉด (Injecting) หรอสรำง object เขำไปยง object ทม

ควำมเกยวของกน (dependencies of objects) เพอใหเกดกำรท ำงำนรวมกนในแอปพลเคชน Dependency Injection แบงออกเปน 2 ประเภท ไดแก Setter Injection และ Constructor

Injection

6

Spring BootSpring Boot ไมใช Framework แตเปนเครองมอ

ทชวยสรำง package และรนแอปพลเคชนทสรำงดวย Spring Framework

มชด Starter Library ใหเรยกใชงำนไดสะดวก ลดขนตอนกำรเพม library อนๆ ทเกยวของ

ม Auto Configuration เพอใหลดขนตอนกำรก ำหนดคำตำงๆ

ม Built-in web server เชน Tomcat ชวยใหสรำงแอปพลเคชนแบบแยกสวนกำรท ำงำนไดงำย

7

Spring Boot Architecture

8

Spring Boot Container

JPA

Hibernate

JDBC Database

Web Container

Spring Data

DAO

DispatcherServlet

Spring Security

RestService

Service

Client

Spring Boot ท ำอะไรไดบำงตวอยำงโคดและค ำอธบำย Spring Boot ในเคสตำงๆ -> https://spring.io/guides

9

Spring Boot application starters

10

ชอ Dependency ค ำอธบำยspring-boot-starter ใชกบแอปพลเคชนทใชเฉพำะสวนหลกของ Spring Boot ในสวน auto-configuration ตำงๆspring-boot-starter-web ใชในกำรพฒนำเวบแบบ full-stack ประกอบดวย Tomcat และ spring-webmvcspring-boot-starter-jdbc ใชกบกำรตดตอฐำนขอมลผำน JDBCspring-boot-starter-data-jpa สนบสนน JPA (Java Persistence API) ประกอบดวย spring-data-jpa, spring-orm และ Hibernatespring-boot-starter-data-mongodb สนบสนน MongoDB NoSQL Databasespring-boot-starter-data-rest ใชพฒนำ Web API (RESTful API) จำก spring-data-rest-webmvcspring-boot-starter-freemarker ใช Render หนำแสดงผลโดยไมใชโคดภำษำจำวำดวย FreeMarker templating enginespring-boot-starter-thymeleaf ใช Render หนำแสดงผลโดยไมใชโคดภำษำจำวำดวย Thymeleaf templating enginespring-boot-starter-ws ใชพฒนำ Web Servicesspring-boot-starter-cloud-connectors ใชตดตอกบ “Spring Cloud Connectors” เชน Cloud Foundry และ Herokuspring-boot-starter-security ใชจดกำรควำมปลอดภยในกำรเขำถงระบบspring-boot-starter-social-facebook ใชตดตอกบ Facebook APIspring-boot-starter-social-twitter ใชตดตอกบ Twitter API

Spring Boot มชดแมแบบ (Starter พรอมใช) แบงตำมประเภทของแอปพลเคชน มชอ artifactId ขนตนดวย spring-boot-starter-* สำมำรถน ำชอ Dependency ไประบในไฟล POM ของโปรเจค Maven ซงจะไดชดLibrary .jar ตำงๆ ทเกยวของมำเกบไวในโปรเจคแบบอตโนมต

กำรสรำง Spring Boot Project

ใชตวสรำงตนแบบจำกเวบ Spring Initializrhttps://start.spring.io/

จะไดไฟลเรมตนส ำหรบน ำไปสรำง Project บนเครองมอทใชพฒนำ เชน Eclipse ในรปแบบ

[ชอโปรเจค].zip

11

Spring Initializr

12

เลอกทไมใช SNAPSHOT

ใสชอ Project

เลอก Version Java ใหตรงกบทตดตง

Spring Initializr

13

เลอกชดแมแบบ1. Spring Boot DevTools2. Spring Web3. Spring Data JPA4. MySQL Driver

download ไฟลเรมตน จะไดไฟล .zip

กำรเปด Project บน Eclipse

14

1. Extract ไฟล .zip ไวทใดกได2. เปดโปรเจค เลอกเมน Open Projects from File System…

เลอกโฟลเดอรทExtract

โครงสรำงกำรเกบไฟล

15

โฟลเดอรส ำหรบเกบคลำสจำวำ

package ส ำหรบเกบ class file

ไฟล Configuration เกยวกบ Spring Boot Application เชนURL ของฐำนขอมล

ไฟล application.propertiesกำร Config คำเกยวกบ Spring Boot Application จะถกก ำหนดในไฟล

application.propertiesกำรเกบขอมลจะอยในรปแบบ

property-name = value

แมแบบ spring-boot-starter-web ม Web Container Tomcat ฝงมำในตวแลว และก ำหนด port เปน 8080 ดงนนควร Stop Server ทเคยเปดอย เพอปองกน port ชนกน หรอเปลยน port ของ Spring Boot Application โดยเพม property ดงน

16

กำร Config คำเกยวกบฐำนขอมล

17

spring.mvc.view.prefix=/jsp/spring.mvc.view.suffix=.jsp

spring.datasource.url = jdbc:mysql://localhost/mydb?characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password =

spring.jpa.show-sql = truespring.jpa.properties.hibernate.format_sql=truespring.jpa.hibernate.ddl-auto = update# spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect

หมำยเหต ภำษำ SQL แบงค ำสงออกเปน 2 สวน1. สวนนยำมขอมล (Data Definition Language: DDL) เชน ค ำสง create/alter/drop table2. สวนจดกำรขอมล (Data Manipulation Language: DML) เชน ค ำสง select, insert, update, delete

ก ำหนดคำส ำหรบทตดตอฐำนขอมลในไฟล application.properties

ก ำหนดใหแสดงค ำสง SQL บน consoletrue - แสดง, false - ไมแสดง

ชอฐำนขอมล

คำ spring.jpa.hibernate.ddl-autoเปนกำรก ำหนดใหสรำงค ำสงส ำหรบสรำงโครงสรำงตำรำงในฐำนขอมล

(DDL) แบบอตโนมต เมอเจอ Entity class ขณะเรม start spring boot คำทก ำหนดไดประกอบดวย none, create-drop, create , update

create-drop – สรำง schema ตำม Entity class ทมและท ำลำยเมอจบกำรท ำงำน

create – สรำง schema ตำม Entity class ทมแตไมท ำลำยเมอจบกำรท ำงำน

update – หำกยงไมเคยสรำง schema จะสรำงกอน ถำ start server ใหมจะตรวจสอบวำ entity มกำรปรบปรงหรอไม ถำมจะเพมใหอตโนมต

18

ตวอยำง property อนๆ

19

# EMBEDDED SERVER CONFIGURATION (ServerProperties)server.port=8080server.address= # bind to a specific NICserver.session-timeout= # session timeout in seconds

# HTTP encoding (HttpEncodingProperties)

# encoding of HTTP requests/responsesspring.http.encoding.charset=UTF-8

# enable http encoding supportspring.http.encoding.enabled=true

# force the configured encodingspring.http.encoding.force=true

ด property อน ๆ ไดจำก https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#json-properties

ทดสอบสรำง Controller

@RestController

public class FirstController {

@GetMapping("/hello")

public String home() {

return "Hello World";

}

}

20

ระบวำใหคลำสนเปน Controller ของ RESTful API

ก ำหนด Path ของ HTTP request ทใชเขำถงเมธอดนแบบ GET

สง HTTP response กลบไปดวยขอควำมธรรมดำ (plain text)

คลำสส ำหรบรน Spring Boot ApplicationSpring Initializr จะสรำงคลำสหลกส ำหรบใชในกำรรน Spring Boot Application มำ

ใหแลว

21

@SpringBootApplication

public class MainApplication {

public static void main(String[] args) {

SpringApplication.run(MainApplication.class, args);

}

}

annotation เพอใหมกำร config แอปพลเคชนแบบอตโนมต

สงให Spring Boot Application เรมท ำงำน ใชรบคำทำง command line

คลำสเรมตนจะมเมธอด main( )

กำรรน Spring Boot Applicationคลกขวำทคลำสหลก เลอก Run As > Java Application

22

กำรรน Spring Boot Application

23

ใช port 8080

ผลลพธจำกกำรสง HTTP Request ผำน Browser

Version ของ Spring Boot ทใช

กำรสรำง Executable jar file (fat jars)Spring Boot Application ทสรำงเสรจแลว สำมำรถน ำไปรนทเครองใดกไดทม JRE

แตตองสรำง Executable jar file ขนมำกอนคลกขวำทชอ Project เลอก Run As > Maven build… ทชอง Goals ใสค ำวำ "package" และกดปม Run

24

ก ำหนดต ำแหนง Java SE

25

1. เลอกแทบ JRE

2. คลก

3. เลอก JavaSEVersion ทตรงกบ Project

4. เรม package

jar file ทไดคลกขวำทชอ Project > Refresh จะเหนไฟล Executable jar file (.jar) ภำยใน

โฟลเดอร target

26

กำรรน Executable jar file คลกขวำทโฟลเดอร target

เลอก Show In > System Explorer

กดปม Shift คำงไวแลวคลกขวำทโฟลเดอร targetเลอก Open command window here

พมพค ำสงเพอเปดกำรท ำงำนไฟล .jar ทสรำงขน รปแบบดงน

java -jar ชอไฟล.jar

27หมำยเหต ควร stop กำรท ำงำนใน Eclipse กอน เพรำะ port จะชนกน