Spring Boot & Framework

2y ago
17 Views
2 Downloads
1.88 MB
27 Pages
Last View : 6d ago
Last Download : 3m ago
Upload by : Joanna Keil
Transcription

Spring Framework & Spring Boot1

Java Framework Technologies2

Java Runtime Platform3

Spring Framework Framework ��บนแพลตฟอร์ มของจำวำ เป้ำหมำยของ Spring คือ ลดเวลำในกำรพัฒนำ และเสนอแนวปฏิบตั ิที่ดีให้กบั นักพัฒนำ เป็ น Open source ที่มีขนำดเล็ก (Lightweight) �ยได้ง่ำย4

Spring Framework5

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 และ ConstructorInjection6

Spring Boot Spring Boot ไม่ใช่ Framework แต่เป็ นเครื่ � package �ที่สร้ำงด้วย Spring Framework มีชุด Starter Library ให้เรี ยกใช้งำนได้สะดวก � library อื่นๆ ที่เกี่ยวข้อง มี Auto Configuration �กำรกำหนดค่ำต่ำงๆ มี Built-in web server เช่น Tomcat �ิเคชันแบบแยกส่ �7

Spring Boot ArchitectureWeb ContainerSpring Boot ContainerServiceDAORestServiceSpring ityJDBCDatabase8

Spring Boot ทำอะไรได้ �ละคำอธิบำย Spring Boot ในเคสต่ำงๆ - https://spring.io/guides9

Spring Boot application startersSpring Boot มีชุดแม่แบบ (Starter พร้อมใช้) �อปพลิเคชัน มีชื่อ artifactId er-* สำมำรถนำชื่อ Dependency ไประบุในไฟล์ POM ของโปรเจค Maven ซึ่งจะได้ชุดLibrary .jar ต่ำงๆ ��ัตโนมัติชื่ อ ยใช้กบั �เฉพำะส่ วนหลักของ Spring Boot ในส่ วน auto-configuration �ำเว็บแบบ full-stack ประกอบด้วย Tomcat และ spring-webmvcใช้กบั �ลผ่ำน JDBCสนับสนุน JPA (Java Persistence API) ประกอบด้วย spring-data-jpa, spring-orm และ Hibernateสนับสนุน MongoDB NoSQL Databaseใช้พฒั นำ Web API (RESTful API) จำก spring-data-rest-webmvcใช้ Render ��ย FreeMarker templating engineใช้ Render ��ย Thymeleaf templating engineใช้พฒั นำ Web Servicesใช้ติดต่อกับ “Spring Cloud Connectors” เช่น Cloud Foundry และ Herokuใช้จดั ��ิดต่อกับ Facebook APIใช้ติดต่อกับ Twitter API10

กำรสร้ ำง Spring Boot Project ใช้ตวั �บ Spring Initializrhttps://start.spring.io/ จะได้ไฟล์เริ่ �ำง Project บนเครื่ องมือที่ใช้พัฒนำ เช่น Eclipse ในรู ปแบบ[ชื่อโปรเจค].zip11

Spring ��SNAPSHOTใส่ ชื่อ Projectเลือก Version Java �้ง12

Spring ��1. Spring Boot DevTools2. Spring Web3. Spring Data JPA4. MySQL Driverdownload ไฟล์เริ่ มต้นจะได้ไฟล์ .zip13

กำรเปิ ด Project บน Eclipse1. Extract ไฟล์ .zip ไว้ที่ใดก็ได้2. เปิ ดโปรเจค เลือกเมนู OpenProjects from File System tract14

โครงสร้ �ดอร์ �ำpackage สำหรับเก็บ class fileไฟล์ Configuration เกีย่ วกับ Spring Boot Application เช่ นURL ของฐำนข้ อมูล15

ไฟล์ application.properties กำร Config ค่ำเกี่ยวกับ Spring Boot Application plication.properties �ูใ่ นรู ปแบบproperty-name value แม่แบบ spring-boot-starter-web มี Web Container Tomcat ฝังมำในตัวแล้ว และกำหนด port เป็ น 8080 ดังนั้นควร Stop Server ที่เคยเปิ ดอยู่ เพื่อป้องกัน port ชนกันหรื อเปลี่ยน port ของ Spring Boot Application โดยเพิ่ม property ดังนี้16

กำร Config ค่ ำเกีย่ วกับฐำนข้ ��ูลในไฟล์ application.propertiesspring.mvc.view.prefix /jsp/spring.mvc.view.suffix datasource.url jdbc:mysql://localhost/mydb?characterEncoding utf-8spring.datasource.username rootspring.datasource.password �่ SQL บน consoletrue - แสดง, false - ไม่แสดงspring.jpa.show-sql truespring.jpa.properties.hibernate.format sql truespring.jpa.hibernate.ddl-auto update# spring.jpa.properties.hibernate.dialect ��ตุ ภำษำ SQL แบ่งคำสัง่ ออกเป็ น 2 ส่ วน1. ส่ วนนิยำมข้อมูล (Data Definition Language: DDL) เช่น คำสัง่ create/alter/drop table2. ส่ วนจัดกำรข้อมูล (Data Manipulation Language: DML) เช่น คำสัง่ select, insert, update, delete17

ค่ ำ 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 �ำ entity มีกำรปรับปรุ งหรื อไม่ �ตโนมัติ18

ตัวอย่ ำง property อื่นๆ# 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 อื่น ๆ ได้จำก json-properties19

ทดสอบสร้ ำง Controllerระบุว่ำให้ คลำสนีเ้ ป็ น Controller ของ RESTful API@RestControllerpublic class FirstController {@GetMapping("/hello")public String home() {return "Hello World";}กำหนด Path ของ HTTP request ที่ใช้ เข้ ำถึงเมธอดนีแ้ บบ GETส่ ง HTTP response กลับไปด้ วยข้ อควำมธรรมดำ (plain text)}20

คลำสสำหรับรัน Spring Boot Application Spring Initializr �หรับใช้ในกำรรัน Spring Boot Application มำให้แล้วannotation เพื่อให้ มีกำร config �โนมัติ@SpringBootApplicationpublic class MainApplication {public static void main(String[] args) {คลำสเริ่มต้ นจะมีเมธอด main( )SpringApplication.run(MainApplication.class, args);}สั่ งให้ Spring Boot Application เริ่มทำงำนใช้ รับค่ ำทำง command line}21

กำรรัน Spring Boot Application �ก เลือก Run As Java Application22

กำรรัน Spring Boot ApplicationVersion ของ Spring Boot ที่ใช้ใช้ port 8080ผลลัพธ์จำกกำรส่ ง HTTP Requestผ่ำน Browser23

กำรสร้ ำง Executable jar file (fat jars) Spring Boot Application ที่สร้ำงเสร็ จแล้ว �ครื่ องใดก็ได้ที่มี JREแต่ตอ้ งสร้ำง Executable jar file ขึ้นมำก่อน คลิกขวำที่ชื่อ Project เลือก Run As Maven build ที่ช่อง Goals ใส่ คำว่ำ "package" และกดปุ่ ม Run24

กำหนดตำแหน่ ง Java SE1. เลือกแท็บ JRE2. คลิก3. เลือก JavaSEVersion ที่ตรงกับ Project4. เริ่ ม package25

jar file ที่ได้ คลิกขวำที่ชื่อ Project Refresh จะเห็นไฟล์ Executable jar file (.jar) ภำยในโฟลเดอร์ target26

กำรรัน Executable jar file �์ targetเลือก Show In System Explorer กดปุ่ ม Shift �ำที่โฟลเดอร์ targetเลือก Open command window here พิมพ์คำสัง่ เพื่อเปิ ดกำรทำงำนไฟล์ .jar ที่สร้ำงขึ้น รู ปแบบดังนี้java -jar �� ควร stop กำรทำงำนใน Eclipse ก่ อน เพรำะ port จะชนกัน27

Spring Boot application starters 10 ชื่อ Dependency ค ำอธิบำย spring-boot-starter ��ักของ Spring Boot ในส่วนauto-configuration ต่ำงๆ spring

Related Documents:

configurations: UEFI Mode and Legacy BIOS Mode. Certain boot options described in this guide require that you select a specific boot mode. By default, the boot mode is set to UEFI Mode. The system must boot in UEFI Mode to use the following options: Secure Boot, UEFI Optimized Boot, Generic USB Boot, IPv6 PXE Boot, iSCSI Boot, and Boot from URL

Contents iv Catalyst 3750 Switch Command Reference OL-8552-01 aaa authorization network 2-5 action 2-6 archive copy-sw 2-8 archive download-sw 2-11 archive tar 2-15 archive upload-sw 2-18 arp access-list 2-20 auto qos voip 2-22 boot auto-copy-sw 2-27 boot boothlpr 2-28 boot config-file 2-29 boot enable-break 2-30 boot helper 2-31 boot helper-config-file 2-32 boot manual 2-33 boot private .

1) Explain the term ‘Spring Boot’. It is a Spring module that offers Rapid Application Development to Spring framework. Spring module is used to create an application based on Spring framework which requires to configure few Spring files. 2) Mention some advantages of Spring Boot Here are som

Boot from EFI File The first method, Boot from EFI File is invoked by pressing the F9 Key to launch Boot Manager. All available boot options are list under the Boot Option Menu. Selecting Boot from EFI File presents the File Explorer Screen which lists all available file system mappings.

Spring Initializr is also integrated with all major Java IDEs along with CLI. 6 Q10. What are the advantages of Spring Boot Initializr? Spring Boot Initializr provides a simple interface to quickly bootstrap a Spring Boot application. Here are some of the benefits or advantages of using Initilizr.

– Make sure that Legacy Boot or UEFI Boot is enabled – Make sure that Secure Boot is disabled 8 Save changes, reboot the computer, and start the BIOS. 9 Under the boot mode (Legacy Boot or UEFI Boot), select the USB flash drive. 10 Follow the on-screen instructions to install Windows 7

Option ROM. In order to PXE boot, the PXE Option ROM must be enabled in the BIOS. The PXE boot method (boot from network) should be set to first in the boot order. Disabling the other boot devices in the BIOS will ensure that the unit will only boot via PXE.

Reading Comprehension Study Guide and Practice Test 2015 Page 6 Part C: Reading Skill: Making Inferences Read the following passage and answer the questions that follow it. Of all the farm animals a person might own, the goat is the best personal pet. For one thing, you can keep it for a longer time than other farm animals. Even after a doe is .