SpringBoot介紹
Spring Boot使創建可運行的獨立,生產級基於Spring的應用程序變得容易。對Spring平台和第三方庫整合,這樣就可以以最小的麻煩開始使用。大多數Spring Boot應用程序只需要很少的Spring配置。
可以使用Spring Boot創建可以通過使用java -jar
或更傳統的戰爭部署啟動的Java應用程序。
主要目標是:
- 為所有Spring開發提供根本上更快且可廣泛訪問的入門體驗。
- 開箱即用,但由於需求開始與默認值有所出入,因此很快就會擺脫困境。
- 提供一系列大型項目通用的非功能性功能(例如嵌入式服務器,安全性,指標,運行狀況檢查和外部化配置)。
- 完全沒有代碼生成,也不需要XML配置。
SpringBoot搭建
環境:Java 1.8
工具:Idea
1、打開idea,創建工程Maven工程
2、根據步驟,填入GroupId和ArtifactId,和項目名稱,完成之后,等於Maven工程創建好了
3、編輯pom.xml文件,添加SpringBoot父工程,和web項目的啟動器依賴,以及打包SpringBoot項目的插件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-helloworld</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 18 <dependencies> 19 <!-- 不需要寫版本號,版本號依賴父項目(spring-boot-starter-parent)管理 --> 20 <!-- SpringBoot 將所有的功能場景抽取出來,做成一個個starter(啟動器), 21 只需要在項目中引入這些starter相關場景的所有依賴都會導入進來,要用什么功能就導入什么啟動器--> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-web</artifactId> 25 </dependency> 26 </dependencies> 27 28 29 <!-- SpringBoot打包插件,可以將代碼打包成一個可執行的jar包 --> 30 <build> 31 <plugins> 32 <plugin> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-maven-plugin</artifactId> 35 </plugin> 36 </plugins> 37 </build> 38 39 </project>
4、增加SpringBoot啟動類,Application.java,內容如下:
1 package com.test.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 7 /** 8 * @SpringBootApplication 用來標注一個主程序,說明這是一個Spring Boot應用 9 */ 10 @SpringBootApplication 11 public class Application { 12 13 public static void main(String[] args) { 14 15 // Spring應用啟動 16 SpringApplication.run(Application.class, args); 17 } 18 }
5、增加一個Controller,用來驗證web訪問是否成功,HelloController.java
1 package com.test.springboot.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.ResponseBody; 6 7 @Controller 8 public class HelloController { 9 10 @RequestMapping("/hello") 11 @ResponseBody 12 public String hello(){ 13 return "Hello World!"; 14 } 15 16 }
6、運行Application.java的主方法,然后瀏覽器輸入地址: http://localhost:8080/hello,進行訪問
啟動控制台輸出如下:
瀏覽器訪問結果如下:
SpringBoot項目部署
1、使用Maven進行打包,可以使用idea的Maven界面快捷打包
2、打包成功后,找target目錄中找到test-springboot-helloworld-1.0-SNAPSHOT.jar包
使用命令運行:java -jar test-springboot-helloworld-1.0-SNAPSHOT.jar
3、瀏覽器輸入地址: http://localhost:8080/hello訪問,效果如上
SpringBoot項目目錄結構
1、登錄springboot官方的網站:https://start.spring.io/,快速搭建一個springboot項目
2、查看目錄,目錄說明如下
src/main/java --- 源代碼目錄
src/main/resources --- 資源文件目錄
static --- 保存所有的靜態資源:js css images
templates --- 保存所有的模版資源:(SpringBoot默認jar包使用嵌入式的Tomcat,默認不支持jsp頁面),可以使用模版引擎(freemarker、thymeleaf)
application.properties --- SpringBoot的配置文件
pom.xml --- maven配置文件