Spring Boot使建立可運行的獨立,生產級基於Spring的應用程序變得容易。對Spring平臺和第三方庫整合,這樣就能夠以最小的麻煩開始使用。大多數Spring Boot應用程序只須要不多的Spring配置。css
可使用Spring Boot建立能夠經過使用java -jar
或更傳統的戰爭部署啓動的Java應用程序。
java
主要目標是:web
環境:Java 1.8spring
工具:Ideaapache
一、打開idea,建立工程Maven工程瀏覽器
二、根據步驟,填入GroupId和ArtifactId,和項目名稱,完成以後,等於Maven工程建立好了安全
三、編輯pom.xml文件,添加SpringBoot父工程,和web項目的啓動器依賴,以及打包SpringBoot項目的插件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>
四、增長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 }
五、增長一個Controller,用來驗證web訪問是否成功,HelloController.javaapp
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 }
六、運行Application.java的主方法,而後瀏覽器輸入地址: http://localhost:8080/hello,進行訪問
啓動控制檯輸出以下:
瀏覽器訪問結果以下:
一、使用Maven進行打包,可使用idea的Maven界面快捷打包
二、打包成功後,找target目錄中找到test-springboot-helloworld-1.0-SNAPSHOT.jar包
使用命令運行:java -jar test-springboot-helloworld-1.0-SNAPSHOT.jar
三、瀏覽器輸入地址: http://localhost:8080/hello訪問,效果如上
一、登陸springboot官方的網站:https://start.spring.io/,快速搭建一個springboot項目
二、查看目錄,目錄說明以下
src/main/java --- 源代碼目錄
src/main/resources --- 資源文件目錄
static --- 保存全部的靜態資源:js css images
templates --- 保存全部的模版資源:(SpringBoot默認jar包使用嵌入式的Tomcat,默認不支持jsp頁面),可使用模版引擎(freemarker、thymeleaf)
application.properties --- SpringBoot的配置文件
pom.xml --- maven配置文件