Spring Boot 概述
Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.
上面是引自官網的一段話,大概是說: Spring Boot 是所有基於 Spring 開發的項目的起點。Spring Boot 的設計是爲了讓你儘可能快的跑起來 Spring 應用程序並且儘可能減少你的配置文件。
什麼是 Spring Boot
- 它使用 “習慣優於配置” (項目中存在大量的配置,此外還內置一個習慣性的配置,讓你無須)的理念讓你的項目快速運行起來。
- 它並不是什麼新的框架,而是默認配置了很多框架的使用方式,就像 Maven 整合了所有的 jar 包一樣,Spring Boot 整合了所有框架(引自:springboot(一):入門篇——純潔的微笑)
使用 Spring Boot 有什麼好處
回顧我們之前的 SSM 項目,搭建過程還是比較繁瑣的,需要:
- 1)配置 web.xml,加載 spring 和 spring mvc
- 2)配置數據庫連接、配置日誌文件
- 3)配置家在配置文件的讀取,開啓註解
- 4)配置mapper文件
- .....
而使用 Spring Boot 來開發項目則只需要非常少的幾個配置就可以搭建起來一個 Web 項目,並且利用 IDEA 可以自動生成生成,這簡直是太爽了...
- 劃重點:簡單、快速、方便地搭建項目;對主流開發框架的無配置集成;極大提高了開發、部署效率。
Spring Boot 快速搭建
第一步:新建項目
選擇 Spring Initializr ,然後選擇默認的 url 點擊【Next】:
然後修改一下項目的信息:
勾選上 Web 模板:
選擇好項目的位置,點擊【Finish】:
如果是第一次配置 Spring Boot 的話可能需要等待一會兒 IDEA 下載相應的 依賴包,默認創建好的項目結構如下:
項目結構還是看上去挺清爽的,少了很多配置文件,我們來了解一下默認生成的有什麼:
- SpringbootApplication: 一個帶有 main() 方法的類,用於啓動應用程序
- SpringbootApplicationTests:一個空的 Junit 測試了,它加載了一個使用 Spring Boot 字典配置功能的 Spring 應用程序上下文
- application.properties:一個空的 properties 文件,可以根據需要添加配置屬性
- pom.xml: Maven 構建說明文件
第二步:HelloController
在【cn.wmyskxz.springboot】包下新建一個【HelloController】:
package cn.wmyskxz.springboot;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 測試控制器
*
* @author: @我沒有三顆心臟
* @create: 2018-05-08-下午 16:46
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
- @RestController 註解: 該註解是 @Controller 和 @ResponseBody 註解的合體版
第三步:利用 IDEA 啓動 Spring Boot
我們回到 SpringbootApplication 這個類中,然後右鍵點擊運行:
- 注意:我們之所以在上面的項目中沒有手動的去配置 Tomcat 服務器,是因爲 Spring Boot 內置了 Tomcat
等待一會兒就會看到下方的成功運行的提示信息:
可以看到我們的 Tomcat 運行在 8080 端口,我們來訪問 “/hello
” 地址試一下:
可以看到頁面成功顯示出我們返回的信息。
解析 Spring Boot 項目
解析 pom.xml 文件
讓我們來看看默認生成的 pom.xml 文件中到底有一些什麼特別:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wmyskxz</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我們可以看到一個比較陌生一些的標籤 <parent>
,這個標籤是在配置 Spring Boot 的父級依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
有了這個,當前的項目纔是 Spring Boot 項目,spring-boot-starter-parent 是一個特殊的 starter ,它用來提供相關的 Maven 默認依賴,使用它之後,常用的包依賴就可以省去 version 標籤。
關於具體 Spring Boot 提供了哪些 jar 包的依賴,我們可以查看本地 Maven 倉庫下:\repository\org\springframework\boot\spring-boot-dependencies\2.0.1.RELEASE\spring-boot-dependencies-2.0.1.RELEASE.pom 文件來查看,挺長的...
應用入口類
Spring Boot 項目通常有一個名爲 *Application 的入口類,入口類裏有一個 main 方法, 這個 main 方法其實就是一個標準的 Javay 應用的入口方法。
@SpringBootApplication 是 Spring Boot 的核心註解,它是一個組合註解,該註解組合了:@Configuration、@EnableAutoConfiguration、@ComponentScan; 若不是用 @SpringBootApplication 註解也可以使用這三個註解代替。
- 其中,@EnableAutoConfiguration 讓 Spring Boot 根據類路徑中的 jar 包依賴爲當前項目進行自動配置,例如,添加了 spring-boot-starter-web 依賴,會自動添加 Tomcat 和 Spring MVC 的依賴,那麼 Spring Boot 會對 Tomcat 和 Spring MVC 進行自動配置。
- Spring Boot 還會自動掃描 @SpringBootApplication 所在類的同級包以及下級包裏的 Bean ,所以入口類建議就配置在 grounpID + arctifactID 組合的包名下(這裏爲 cn.wmyskxz.springboot 包)
Spring Boot 的配置文件
Spring Boot 使用一個全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目錄或者類路徑的 /config 下。
Spring Boot 不僅支持常規的 properties 配置文件,還支持 yaml 語言的配置文件。yaml 是以數據爲中心的語言,在配置數據的時候具有面向對象的特徵。
Spring Boot 的全局配置文件的作用是對一些默認配置的配置值進行修改。
- 簡單實例一下
我們同樣的將 Tomcat 默認端口設置爲 8080 ,並將默認的訪問路徑從 “/
” 修改爲 “/hello
” 時,使用 properties 文件和 yml 文件的區別如上圖。
- 注意: yml 需要在 “
:
” 後加一個空格,幸好 IDEA 很好地支持了 yml 文件的格式有良好的代碼提示;
- 我們可以自己配置多個屬性
我們直接把 .properties 後綴的文件刪掉,使用 .yml 文件來進行簡單的配置,然後使用 @Value 來獲取配置屬性:
重啓 Spring Boot ,輸入地址:localhost:8080/hello 能看到正確的結果:
- 注意: 我們並沒有在 yml 文件中註明屬性的類型,而是在使用的時候定義的。
你也可以在配置文件中使用當前配置:
仍然可以得到正確的結果:
- 問題: 這樣寫配置文件繁瑣而且可能會造成類的臃腫,因爲有許許多多的 @Value 註解。
- 封裝配置信息
我們可以把配置信息封裝成一個類,首先在我們的 name 和 age 前加一個 student 前綴,然後新建一個 StudentProperties 的類用來封裝這些信息,並用上兩個註解:
- @Component:表明當前類是一個 Java Bean
- @ConfigurationProperties(prefix = "student"):表示獲取前綴爲 sutdent 的配置信息
這樣我們就可以在控制器中使用,重啓得到正確信息:
Spring Boot 熱部署
在目前的 Spring Boot 項目中,當發生了任何修改之後我們都需要重新啓動才能夠正確的得到效果,這樣會略顯麻煩,Spring Boot 提供了熱部署的方式,當發現任何類發生了改變,就會通過 JVM 類加載的方式,加載最新的類到虛擬機中,這樣就不需要重新啓動也能看到修改後的效果了。
- 做法也很簡單,修改 pom.xml 即可!
我們往 pom.xml 中添加一個依賴就可以了:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 這個需要爲 true 熱部署纔有效 -->
</dependency>
重新啓動 Spring Boot ,然後修改任意代碼,就能觀察到控制檯的自動重啓現象:
關於如何在 IDEA 中配置熱部署:傳送門
Spring Boot 使用
上面已經完成了 Spring Boot 項目的簡單搭建,我們僅僅需要進行一些簡單的設置,寫一個 HelloController 就能夠直接運行了,不要太簡單...接下來我們再深入瞭解一下 Spring Boot 的使用。
Spring Boot 支持 JSP
Spring Boot 的默認視圖支持是 Thymeleaf 模板引擎,但是這個我們不熟悉啊,我們還是想要使用 JSP 怎麼辦呢?
- 第一步:修改 pom.xml 增加對 JSP 文件的支持
<!-- servlet依賴. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat的支持.-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
- 第二步:配置試圖重定向 JSP 文件的位置
修改 application.yml 文件,將我們的 JSP 文件重定向到 /WEB-INF/views/ 目錄下:
- 第三步:修改 HelloController
修改 @RestController 註解爲 @Controller ,然後將 hello 方法修改爲:
- 第四步:新建 hello.jsp 文件
在【src/main】目錄下依次創建 webapp、WEB-INF、views 目錄,並創建一個 hello.jsp 文件:
- 第五步:刷新網頁
因爲我們部署了熱部署功能,所以只需要等待控制檯重啓信息完成之後再刷新網頁就可以看到正確效果了:
- 關於 404,使用 spring-boot:run 運行項目可以解決:
集成 MyBatis
- 第一步:修改 pom.xml 增加對 MySql和 MyBatis 的支持
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
- 第二步:新增數據庫鏈接參數
這裏我們就直接使用之前創建好的 student 表了吧:
- 第三步:創建 Student 實體類和 StudentMapper 映射類
在【cn.wmyskxz.springboot】下新建一個【pojo】包,然後在其下創建一個 Student 類:
public class Student {
private Integer id;
private Integer student_id;
private String name;
private Integer age;
private String sex;
private Date birthday;
/* getter and setter */
}
在【cn.wmyskxz.springboot】下新建一個【mapper】包,然後在其下創建一個 StudentMapper 映射類:
package cn.wmyskxz.springboot.mapper;
import cn.wmyskxz.springboot.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student")
List<Student> findAll();
}
- 第四步:編寫 StudentController
在【cn.wmyskxz.springboot】下新建一個【controller】包,然後在其下創建一個 StudentController :
package cn.wmyskxz.springboot.controller;
import cn.wmyskxz.springboot.mapper.StudentMapper;
import cn.wmyskxz.springboot.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* Student 控制器
*
* @author: @我沒有三顆心臟
* @create: 2018-05-08-下午 20:25
*/
@Controller
public class StudentController {
@Autowired
StudentMapper studentMapper;
@RequestMapping("/listStudent")
public String listStudent(Model model) {
List<Student> students = studentMapper.findAll();
model.addAttribute("students", students);
return "listStudent";
}
}
第五步:編寫 listStudent.jsp 文件
我們簡化一下 JSP 的文件,僅顯示兩個字段的數據:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<table align='center' border='1' cellspacing='0'>
<tr>
<td>id</td>
<td>name</td>
</tr>
<c:forEach items="${students}" var="s" varStatus="st">
<tr>
<td>${s.id}</td>
<td>${s.name}</td>
</tr>
</c:forEach>
</table>
- 第六步:重啓服務器運行
因爲往 pom.xml 中新增加了依賴的包,所以自動重啓服務器沒有作用,我們需要手動重啓一次,然後在地址輸入:localhost:8080/listStudent 查看效果:
以上。
參考資料:
how2j.cn-Spring Boot 系列教程