1. 程式人生 > >SpringBoot_第二章(案例入門)

SpringBoot_第二章(案例入門)

前言:在上一章已經對於SpringBoot概念和開發工具配置進行了準備,這一章開始搭建SpringBoot的專案入門,開發工具是eclipse.

1:新建專案

然後建立專案名稱為SpringBoot3,點選下一步

進入到一下頁面,選擇web,勾選上

點選完成,之後再eclipse中就能看到新建的SpringBoot3的專案了,稍等eclipse下載相關的jar,然後就能看到專案的完成目錄結構

如果是第一次配置 Spring Boot 的話可能需要等待一會兒 IDE下載相應的 依賴包.

 

 

專案結構還是看上去挺清爽的,少了很多配置檔案,我們來了解一下預設生成的有什麼:

  • Springboot3Application: 一個帶有 main() 方法的類,用於啟動應用程式
  • Springboot3ApplicationTests:一個空的 Junit 測試了,它載入了一個使用 Spring Boot 字典配置功能的 Spring 應用程式上下文
  • application.properties:一個空的 properties 檔案,可以根據需要新增配置屬性
  • pom.xml: Maven 構建說明檔案

2:寫一個簡單的HelloController

package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot!";
    }
}
  • @RestController 註解: 該註解是 @Controller 和 @ResponseBody 註解的合體版(如果只單用@Controller,頁面無返回值)

3:啟動SringBoot

在這個類中SpringBoot3Application,右鍵 run java application 然後就能看到控制檯輸出(圖太大了截不到,這是網上的圖)

 

然後在瀏覽器訪問地址如下,這就是一個最簡單的SpringBoot入門案例。

 

------------------------------------------------------------------------------分割線解析SpringBoot專案------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.解析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>com.example</groupId>
	<artifactId>SpringBoot3</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBoot3</name>
	<description>SpringBoot2(sts外掛生成)</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.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 標籤。

2.應用入口類

Spring Boot 專案通常有一個名為 *Application 的入口類,入口類裡有一個 main 方法, 這個 main 方法其實就是一個標準的 Java 應用的入口方法。

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
//@ComponentScan(basePackages= {"com.*.*"}) 
//ComponentScan用於載入指定的自定義包,預設載入當前包的子包
public class SpringBoot3Application {
	public static void main(String[] args) {
		SpringApplication.run(SpringBoot3Application.class, args);
	}
}
  • @ComponentScan(basePackages= {"com.example.*"}) 
  • ComponentScan用於載入指定的自定義包,預設載入當前包的子包
  • @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 包)

3:Spring Boot 的配置檔案

Spring Boot 使用一個全域性的配置檔案 application.properties 或 application.yml,放置在【src/main/resources】目錄或者類路徑的 /config 下。這兩種配置檔案實際上沒什麼大的區別,這是格式不一樣,yml便於根據不同的開發測試生產環境,引入其對應環境的yml檔案

Spring Boot 不僅支援常規的 properties 配置檔案,還支援 yml 語言的配置檔案。yml 是以資料為中心的語言,在配置資料的時候具有面向物件的特徵。

Spring Boot 的全域性配置檔案的作用是對一些預設配置的配置值進行修改。

我們直接把 .properties 字尾的檔案刪掉,使用 .yml 檔案來進行簡單的配置,然後使用 @Value 來獲取配置屬性:

重啟 Spring Boot ,輸入地址:能看到正確的結果:

你也可以在配置檔案中使用當前配置: 

  • 注意: 我們並沒有在 yml 檔案中註明屬性的型別,而是在使用的時候定義的。

 

仍然可以得到正確的結果:

  • 問題: 這樣寫配置檔案繁瑣而且可能會造成類的臃腫,因為有許許多多的 @Value 註解

我們可以把配置資訊封裝成一個類,首先在我們的 name 和 age 前加一個 student 字首,然後新建一個 StudentProperties 的類用來封裝這些資訊,並用上兩個註解:

  • @Component:表明當前類是一個 Java Bean
  • @ConfigurationProperties(prefix = "student"):表示獲取字首為 sutdent 的配置資訊

 這樣我們就可以在控制器中使用,重啟得到正確資訊: 

3:SpringBoot熱部署 

在目前的 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>

隨意更改專案,不要重啟,即可檢視最新的效果