海南省人民政府網(wǎng)站官網(wǎng)寧波百度快照優(yōu)化排名
目錄
一、SpringBoot 簡(jiǎn)介
1、Spring 的缺點(diǎn)
2、SpringBoot 功能
二、SpringBoot 入門案例
1、實(shí)現(xiàn)步驟
2、訪問(wèn)服務(wù)器
3、入門小結(jié)
4、Idea 快速構(gòu)建 SpringBoot 工程
5、起步依賴無(wú)需版本號(hào)
6、主啟動(dòng)類的在項(xiàng)目中的位置(*重要*)
三、SpringBoot 配置
1、配置文件分類
2、yaml 基本介紹
3、yaml 基本語(yǔ)法
4、yaml 數(shù)據(jù)格式
四、讀取配置文件內(nèi)容
1、@Value
2、Environment 類
3、@ConfigurationProperties
4、讀取配置文件的問(wèn)題匯總
五、Profile
1、為什么需要 profile
2、profile 的配置方式
3、profile 激活方式之多 profile 文件
4、profile 激活方式之 yml 多文檔
5、虛擬機(jī)參數(shù)與命令行參數(shù)
6、激活 profile 問(wèn)題匯總
7、項(xiàng)目?jī)?nèi)部配置文件加載順序
六、SpringBoot 整合其他框架
1、整合 Junit
2、整合 Redis
3、整合 MyBatis(演示注解開(kāi)發(fā))
4、框架整合問(wèn)題匯總
一、SpringBoot 簡(jiǎn)介
1、Spring 的缺點(diǎn)
(1)配置繁瑣
- Spring 的組件代碼是輕量級(jí)的,但是它的配置文件是重量級(jí)的。
- 在思考 Spring 特性配置和解決業(yè)務(wù)問(wèn)題之間需要進(jìn)行思維切換,所以編寫配置擠占了編寫程序的時(shí)間。
(2)依賴繁瑣
- 自己導(dǎo)入 maven 很可能會(huì)導(dǎo)致依賴沖突,還得自行判斷版本號(hào)。
2、SpringBoot 功能
(1)自動(dòng)配置(核心)
- SpringBoot 的自動(dòng)配置是一個(gè)運(yùn)行時(shí)(更準(zhǔn)確地說(shuō),是應(yīng)用程序啟動(dòng)時(shí))的過(guò)程,考慮了眾多因素,才決定 Spring 配置應(yīng)該用哪個(gè),不該用哪個(gè)。該過(guò)程是 SpringBoot 自動(dòng)完成的。
(2)起步依賴(核心)
- 起步依賴本質(zhì)上是一個(gè) Maven 項(xiàng)目對(duì)象模型(Project Object Model,POM),定義了對(duì)其他庫(kù)的傳遞依賴,這些東西加在一起即支持某項(xiàng)功能。
- 簡(jiǎn)單的說(shuō),起步依賴就是將具備某種功能的坐標(biāo)打包到一起,并提供一些默認(rèn)的功能。
(3)輔助功能
- 提供了一些大型項(xiàng)目中常見(jiàn)的非功能性特性,如嵌入式服務(wù)器、安全、指標(biāo),健康檢測(cè)、外部配置等。就比如 Tomcat 服務(wù)器,不需要再手動(dòng)配置了。
總的來(lái)說(shuō),SpringBoot 提供了一種快速開(kāi)發(fā) Spring 項(xiàng)目的方式,而不是對(duì) Spring 功能上的增強(qiáng)。
二、SpringBoot 入門案例
搭建 SpringBoot 工程,定義 HelloController.hello() 方法,返回 ”Hello SpringBoot!”。
1、實(shí)現(xiàn)步驟
在整個(gè)步驟中,不需要寫一行配置,沒(méi)有引入 Tomcat 的插件。
(1)導(dǎo)入 SpringBoot 起步依賴
- 我的 JDK 是 16 版本,只適合用 2.7.3,根據(jù)自己的 JDK 版本選擇。?
<!-- SpringBoot 需要繼承的父工程 -->
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.3</version>
</parent><dependencies><!-- web 開(kāi)發(fā)的起步依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 測(cè)試 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
</dependencies>
(2)定義 Controller
- 需要注意的是,由于我們沒(méi)有定義視圖解析器,所以會(huì)導(dǎo)致 404。
- 既然沒(méi)有解析器和對(duì)應(yīng) html,那么可以將返回值顯示到 /hello 的頁(yè)面上,加上 @ResponseBody 即可。
package com.demo.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
public class HelloController {@RequestMapping(value = "/hello")@ResponseBodypublic String hello() { // 在 /hello 頁(yè)面輸出 hello, springboot!System.out.println("/hello");return "hello, springboot!";}
}
(3)編寫引導(dǎo)類
- 引導(dǎo)類一般以 Application 作為后綴。
- 這是 SpringBoot 的入口。
package com.demo.application;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class HelloApplication {}
(4)編寫測(cè)試代碼
- 測(cè)試類(假的,別用)
public class HelloApplicationTest {@Testpublic void test1() {SpringApplication.run(HelloApplication.class);}
}
- 在實(shí)際使用中,我們需要在 main 方法中執(zhí)行 run 方法,因?yàn)榉?wù)器需要保持運(yùn)行
- 也就是在引導(dǎo)類中的 main 方法,調(diào)用 run 方法
package com.demo.application;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class HelloApplication {public static void main(String[] args) {SpringApplication.run(HelloApplication.class, args);}
}
2、訪問(wèn)服務(wù)器
當(dāng)我們?cè)?main 方法中調(diào)用 run 方法后,該如何訪問(wèn)服務(wù)器呢?
在輸出的信息中其實(shí)有這么一些信息:
顯然 Tomcat 服務(wù)器已經(jīng)在 8080 端口開(kāi)放了,直接訪問(wèn)即可。
3、入門小結(jié)
- SpringBoot 在創(chuàng)建項(xiàng)目時(shí),使用 jar 的打包方式。
- SpringBoot 的引導(dǎo)類,是項(xiàng)目入口,運(yùn)行 main 方法就可以啟動(dòng)項(xiàng)目。
- 使用 SpringBoot 和 Spring 構(gòu)建的項(xiàng)目,業(yè)務(wù)代碼編與方式完全一樣。
4、Idea 快速構(gòu)建 SpringBoot 工程
如果我們連工程需要引入什么樣的依賴都不想自己動(dòng)手寫,那么可以直接使用 SpringBoot 的生成器。但是需要一個(gè)聯(lián)網(wǎng)環(huán)境。
(1)新建,選擇?SpringInitial
- 服務(wù)器 URL:是 Spring 構(gòu)建工程的服務(wù)器。
(2)直接選擇我們需要的依賴
(3)項(xiàng)目結(jié)構(gòu)
5、起步依賴無(wú)需版本號(hào)
在入門案例中,我們寫的 POM 文件里,spring-boot-starter-web 是沒(méi)有規(guī)定版本號(hào)的。
這是因?yàn)?#xff0c;在 SpringBoot 的父工程里,已經(jīng)設(shè)定了大量的依賴的版本號(hào),這也是 SpringBoot 能防止依賴沖突的原因之一。
6、主啟動(dòng)類的在項(xiàng)目中的位置(*重要*)
這是非常重要的一個(gè)問(wèn)題,主啟動(dòng)類的位置決定了 SpringBoot 自動(dòng)掃描的范圍。
- 如果其他框架的包(如 mapper),不在主啟動(dòng)類的同目錄、子目錄下,那么 SpringBoot 自動(dòng)掃描是找不到其他框架的包的。
- 要么將主啟動(dòng)類放置在父目錄;
- 要么在主啟動(dòng)類中進(jìn)行包掃描(缺點(diǎn)就是各類框架掃描注解不一樣);
三、SpringBoot 配置
1、配置文件分類
SpringBoot是基于約定的,所以很多配置都有默認(rèn)值,但如果想使用自己的配置替換默認(rèn)配置的話,就可以使用 application.properties 或者application.yml (application.yaml)進(jìn)行配置。(yml 和 yaml 是一樣的)
(1)語(yǔ)法的大致區(qū)別
- properties:
prop.driver=110
- yaml:
prop:port: 110
- yaml 的縮進(jìn)以及 : 后的空格都有講究。
(2)簡(jiǎn)單使用
- 當(dāng)我們?cè)?properties 中,將端口號(hào)修改為 8088,就會(huì)發(fā)現(xiàn)可以從 8088 訪問(wèn)服務(wù)器了。
- 還可以配置自定義屬性,只不過(guò)需要我們手動(dòng)加載這部分內(nèi)容。
(3)加載順序(優(yōu)先級(jí))
當(dāng) properties、yml、yaml 都存在 application 配置文件時(shí),加載順序?yàn)?#xff1a;
properties > yml > yaml
此時(shí),在低優(yōu)先級(jí)中的同一個(gè)名稱的屬性就會(huì)失效。
2、yaml 基本介紹
YAML全稱是YAML Ain't Markup Language。
- YAML是一種直觀的能夠被電腦識(shí)別的的數(shù)據(jù)數(shù)據(jù)序列化格式;
- 容易被人類閱讀,容易和腳本語(yǔ)言交互,可以被支持 YAML 庫(kù)的不同的編程語(yǔ)言程序?qū)?#xff1b;
- YAML 文件的擴(kuò)展名可以使用 .yml 或者 .yaml;
- 特點(diǎn):簡(jiǎn)潔,以數(shù)據(jù)為核心;
可以發(fā)現(xiàn),properties 不方便看出層級(jí)關(guān)系;xml 能看出層級(jí)關(guān)系,但是寫標(biāo)簽繁瑣;yaml 只需要相同縮進(jìn),就能得出層級(jí)關(guān)系。
3、yaml 基本語(yǔ)法
4、yaml 數(shù)據(jù)格式
(1)示例
(2)參數(shù)引用
- 使用 ${ } 可以引用其他對(duì)象。?
四、讀取配置文件內(nèi)容
讀取配置文件的方法有好幾種:
- @Value
- Environment 類
- @ConfigurationProperties
1、@Value
具體使用:https://blog.csdn.net/qq_31960623/article/details/116902786
(1)application.yml 文件:
address:- beijing- wuhan- shanghaimyUser:name: wytage: 20hobby:- c++- python- java:web: 'java \n web'spring: "spring \n springboot"address: ${address[1]}
(2)Controller 代碼:
@Value(value = "${myUser.hobby[2].java.web}")
private String web;@Value(value = "${myUser.hobby[2].java.spring}")
private String spring;@Value(value = "${myUser.address}")
private String address;@RequestMapping(value = "/testAtValue")
@ResponseBody
public String testAtValue(@Value(value = "${myUser.name}") String username) {System.out.println("username = " + username);System.out.println("web = " + web);System.out.println("spring = " + spring);System.out.println("address = " + address);return "testAtValue";
}
(3)輸出結(jié)果:
(4)結(jié)果分析:
- 從 web 和 spring 的獲取中我們發(fā)現(xiàn),數(shù)組中的對(duì)象,在使用 [ ] 后,還需要 .點(diǎn)運(yùn)算 才能的到對(duì)象;
- 從 web 和 spring 的輸出來(lái)看,單引號(hào)不會(huì)識(shí)別轉(zhuǎn)義字符;
- 從 address 的獲取來(lái)看,yml 可以實(shí)現(xiàn)參數(shù)引用;
2、Environment 類
- 從 @Value 的使用中可以發(fā)現(xiàn),當(dāng)參數(shù)很多時(shí),使用起來(lái)就非常麻煩,要為每一個(gè)屬性都加上 @Value。
- 而使用 Environment 只需要調(diào)用一個(gè) getProperty() 方法,就可以獲取到配置參數(shù)。
- Environment 是 Spring 提供的,直接 @Autowired 注入即可。
(1)Controller 代碼:
@Autowired
private Environment environment;@RequestMapping(value = "/testEnvironment")
@ResponseBody
public String testEnvironment() {for (int i = 0; i <= 2; ++ i) {System.out.println("address1 = " + environment.getProperty("address[" + i + "]"));}return "testEnvironment";
}
3、@ConfigurationProperties
使用這個(gè)注解,可以將配置文件中的對(duì)象,對(duì)應(yīng)地注入到一個(gè) POJO 對(duì)象中。
(1)編寫 POJO 實(shí)體類
- @Component:讓 Spring 識(shí)別,將其作為一個(gè) bean 加入 IOC 中;
- @ConfigurationProperties(prefix = "person"):設(shè)置一個(gè)前綴,標(biāo)識(shí)該 POJO 類的屬性應(yīng)該從 yml 的哪一個(gè)對(duì)象中獲取值;
- get 和 set 方法不要忘記了;
package com.demo.pojo;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String name;private Integer age;private String[] address;
}
(2)application.yml 文件:
(3)測(cè)試代碼:
@Autowired
private Person person;@RequestMapping(value = "/testConfiguration")
@ResponseBody
public String testConfiguration() {System.out.println("name = " + person.getName());System.out.println("age = " + person.getAge());for (String s : person.getAddress()) {System.out.println("address = " + s);}return "testConfiguration";
}
(4)輸出結(jié)果
(5)給 yml 添加提示功能
<!-- 配置 配置文件的處理器 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
- 添加這個(gè)依賴后,我們?cè)?yml 中寫配置信息時(shí),就可以得到提示。
4、讀取配置文件的問(wèn)題匯總
(1)username
或許有一個(gè)疑問(wèn),在上面 @Value 中的 yml 配置文件里,為什么寫 myUser,而不是直接寫個(gè) user 呢?
- 如果嘗試過(guò)就知道,輸出 user.name,得到的居然是個(gè)人操作系統(tǒng)當(dāng)前的用戶名。
- 顯然 user 作為了一個(gè)保留的關(guān)鍵字,因此當(dāng)我們以后實(shí)際開(kāi)發(fā)中,最好給 user 加上前綴。
像數(shù)據(jù)庫(kù)連接池需要使用 username 時(shí),就要考慮這個(gè)問(wèn)題了:https://blog.csdn.net/weixin_48841931/article/details/126671315
(2)?@ConfigurationProperties 前綴命名問(wèn)題
- prefix 的命名是由要求的。
https://blog.csdn.net/a2664181446/article/details/122581439
(3)@ConfigurationProperties 添加后,yml 還是沒(méi)有提示
- 首先嘗試重新 Build 項(xiàng)目,根據(jù)報(bào)錯(cuò)信息進(jìn)行 Googel。
其中一種原因是編碼問(wèn)題:https://blog.csdn.net/gaogzhen/article/details/107348314
五、Profile
1、為什么需要 profile
我們?cè)陂_(kāi)發(fā) SpringBoot 應(yīng)用時(shí),通常同一套程序要經(jīng)過(guò)好幾個(gè)環(huán)節(jié),會(huì)被安裝到不同的環(huán)境。
不同環(huán)境(開(kāi)發(fā)、聯(lián)調(diào)、預(yù)發(fā)、正式等)所需的配置不同,如果每改變一個(gè)環(huán)境就更改配置不但麻煩(修改代碼、重新構(gòu)建)而且容易出錯(cuò)。
profile 的功能就是來(lái)進(jìn)行動(dòng)態(tài)配置切換的。
2、profile 的配置方式
(1)多 profile 文件方式(properties),也是多 yml 文件方式
(2)yml 多文檔方式(不是多 yml 文件方式)
3、profile 激活方式之多 profile 文件
profile 的激活方式有三種:配置文件、虛擬機(jī)參數(shù)、命令行參數(shù)。
下面以?配置文件?為例子,說(shuō)明 profile 激活。
(1)創(chuàng)建配置文件
配置各個(gè)環(huán)境下對(duì)應(yīng)的 properties 配置文件,并為他們?cè)O(shè)置不同的服務(wù)器端口( server.port = xxxx)。各后綴意義如下:
- develoment:表示開(kāi)發(fā)環(huán)境;
- product:表示生產(chǎn)環(huán)境;
- test:表示測(cè)試環(huán)境;
(2)激活配置
當(dāng)我們創(chuàng)建好這幾個(gè)配置文件后,啟動(dòng) SpringBoot,會(huì)發(fā)現(xiàn)服務(wù)器端口還是 8080。這是因?yàn)檫€沒(méi)有激活任一配置。
- 在 application.properties 中,使用?spring.profiles.active=后綴,就可激活指定的配置文件:
- 為 development 設(shè)置端口為 8081:?
- 啟動(dòng) SpringBoot,此時(shí)端口已經(jīng)改變:?
4、profile 激活方式之 yml 多文檔
(1)創(chuàng)建配置文件
- 只需要一個(gè) application.yml 即可,在其中使用 --- 來(lái)區(qū)分不同的配置。
- 每一個(gè) --- 開(kāi)頭,表示一個(gè)新的配置區(qū)域。
- spring.config.activate.on-profile 用來(lái)表示配置的名稱,作用于前面的“后綴”類似。
- 舊版本使用 spring.profiles 來(lái)表示配置名稱。
(2)激活配置
- 同樣也是 spring.profiles.active 來(lái)激活指定配置。
(3)文件代碼
---
server:port: 8081
spring:config:activate:on-profile: development
---
server:port: 8082
spring:config:activate:on-profile: product
---
server:port: 8083
spring:config:activate:on-profile: test
---
spring:profiles:active: development
(4)啟動(dòng)結(jié)果
- 激活 development 配置,端口應(yīng)為 8081。
5、虛擬機(jī)參數(shù)與命令行參數(shù)
(1)虛擬機(jī)參數(shù)
- 在 VM 選項(xiàng)中,輸入 -Dspring.profiles.active=后綴,即可激活對(duì)應(yīng)配置。?
(2)命令行參數(shù)
- 將我們當(dāng)前的 SpringBoot 工程打包成 jar(可以使用 maven 的 package);
- 在命令行窗口中,先 cd 到 jar 的目錄下,然后運(yùn)行命令:java? -jar? xxx.jar? --spring.profiles.active=development 即可;
- application.properties 和 appliaction.yml 都可以通過(guò)這種方式修改?spring.profiles.active 的值;
6、激活 profile 問(wèn)題匯總
(1)Demo-Profile-0.0.1-SNAPSHOT.jar中沒(méi)有主清單屬性
- 請(qǐng)確保 pom.xml 中有一下內(nèi)容:
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
7、項(xiàng)目?jī)?nèi)部配置文件加載順序
六、SpringBoot 整合其他框架
1、整合 Junit
(1)引入依賴
- 引入 SpringBoot 的起步依賴 test。
- 引入 Junit。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope>
</dependency>
(2)創(chuàng)建測(cè)試類(一個(gè) Service)
(3)@RunWith 注解作用
@RunWith 就是一個(gè)運(yùn)行器;
- @RunWith(JUnit4.class)就是指用 JUnit4 來(lái)運(yùn)行;
- @RunWith(SpringJUnit4ClassRunner.class),讓測(cè)試運(yùn)行于 Spring 測(cè)試環(huán)境,以便在測(cè)試開(kāi)始的時(shí)候自動(dòng)創(chuàng)建 Spring 的應(yīng)用上下文;
- @RunWith(Suite.class)的話就是一套測(cè)試集合;
(4)@SpringBootTest(classes = 啟動(dòng)類名稱.class)
基本等同于啟動(dòng)了整個(gè)服務(wù),此時(shí)便可以開(kāi)始功能測(cè)試。
- 如果注解 @SpringBootTest(classes = 啟動(dòng)類名稱.class) 中配置了項(xiàng)目啟動(dòng)類,則 test 測(cè)試類可以放在 test 下任何包中;
- 如果注解 @SpringBootTest 沒(méi)有配置參數(shù) classes = Application.class,則需要確保 test 測(cè)試類,在啟動(dòng)類的同目錄或者子目錄下;(網(wǎng)上一堆說(shuō)是包路徑一致的,都是片面的)
2、整合 Redis
Redis5 安裝:https://github.com/tporadowski/redis/releases
(1)引入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
(2)本機(jī)地址測(cè)試
- 先不配置遠(yuǎn)程,使用本機(jī)測(cè)試。
- 運(yùn)行之前先啟動(dòng) redis 本地服務(wù)器:redie-server.exe
package com.demo.demoredis;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest
class DemoRedisApplicationTests {@Autowired// 本機(jī)才不用配置端口等信息private RedisTemplate redisTemplate;@Testvoid contextLoads() {}@Testpublic void testSet() {redisTemplate.boundValueOps("name").set("wyt");}@Testpublic void testGet() {System.out.println(redisTemplate.boundValueOps("name").get());}}
(3)通過(guò)配置文件修改 ip 和 port
- 修改 properties 或者 yml 都可以。?
3、整合 MyBatis(演示注解開(kāi)發(fā))
(1)引入 MyBatis 起步依賴、PostgreSQL 驅(qū)動(dòng)依賴
- MyBatis 起步依賴是由 MyBatis 官方編寫的,Spring 沒(méi)有提供;
- 根據(jù)自己所用的數(shù)據(jù)庫(kù)更改 SQL 驅(qū)動(dòng);
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version>
</dependency><dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope>
</dependency>
(2)建立相關(guān)表信息
(3)配置數(shù)據(jù)源
- 在配置文件中配置數(shù)據(jù)源的四個(gè)參數(shù):driver、url、username、password。
(4)注解開(kāi)發(fā)
- 注解開(kāi)發(fā)則沒(méi)有使用 Spring 配置文件,mapper 接口可以用 @Mapper 來(lái)識(shí)別;
- Spring 配置文件中用包掃描的方式將所有 mapper 都生成了 bean 對(duì)象,因此在這里用個(gè) @Repository 來(lái)標(biāo)識(shí);
@Mapper
@Repository
public interface UserMapper {@Select("select * from \"MyUser\";")List<User> queryForAll();
}
(5)測(cè)試及其結(jié)果
@Test
public void test() {List<User> userList = userMapper.queryForAll();System.out.println(userList);
}
(6)注解開(kāi)發(fā)小結(jié)
- From:https://blog.csdn.net/weixin_43591980/article/details/110043008
- 參考:https://blog.csdn.net/qq_40598321/article/details/117730759?
4、框架整合問(wèn)題匯總
(1)是否必須添加 @RunWith 注解
其實(shí)不一定非要添加,主要看導(dǎo)入的 @Test 的包是哪一個(gè):https://www.bmabk.com/index.php/post/121982.html
?