目錄
- SpringBoot 介紹
- 什麼是 SpringBoot ?
- Spring 缺點
- SpringBoot 功能
- SpringBoot 快速入門
- SpringBoot 起步依賴-原理概述
- SpringBoot 配置檔案
- 配置檔案類型
- YAML 介紹
- 讀取配置内容
- profile
- 内部配置檔案加載順序
- 外部配置加載順序
- SpringBoot 整合其他架構
- 整合 Junit
- 整合 Redis
- 整合 Mybatis
- 注解版
- 配置版
SpringBoot 提供了一種快速使用 Spring 的方式,基于約定優于配置的思想,可以讓開發人員不必在配置與邏輯業務之間進行思維的切換,全身心的投入到邏輯業務的代碼編寫中,進而大大提高了開發的效率,一定程度上縮短了項目周期。
2014 年 4 月,Spring Boot 1.0.0 釋出,并作為 Spring 的頂級項目之一。
1)配置繁瑣
- 雖然 Spring 的元件代碼是輕量級的,但它的配置卻是重量級的。一開始,Spring 用 XML 配置,而且是很多 XML 配置。Spring 2.5 引入了基于注解的元件掃描,這消除了大量針對應用程式自身元件的顯式 XML 配置。Spring 3.0 引入了基于 Java 的配置,這是一種類型安全的可重構配置方式,可以代替 XML。
- 所有這些配置都代表了開發時的損耗。因為在思考 Spring 特性配置和解決業務問題之間需要進行思維切換,是以編寫配置擠占了編寫應用程式邏輯的時間。
- 和所有架構一樣,Spring 實用,但它要求的回報也不少。
2)依賴繁瑣
- 項目的依賴管理也是一件耗時耗力的事情。在環境搭建時,需要分析要導入哪些庫的坐标,而且還需要分析導入與之有依賴關系的其他庫的坐标,一旦選錯了依賴的版本,随之而來的不相容問題就會嚴重阻礙項目的開發進度。
1)自動配置
- SpringBoot 的自動配置是一個運作時(更準确地說,是應用程式啟動時)的過程,考慮了衆多因素,才決定 Spring 配置應該用哪個,不該用哪個。該過程是 SpringBoot 自動完成的。
2)起步依賴(依賴傳遞)
- 起步依賴本質上是一個 Maven 項目對象模型(Project Object Model,POM),定義了對其他庫的傳遞依賴,這些東西加在一起即支援某項功能。
- 簡單的說,起步依賴就是将具備某種功能的坐标打包到一起,并提供一些預設的功能。
3)輔助功能
- 提供了一些大型項目中常見的非功能性特性,如嵌入式伺服器、安全、名額,健康檢測、外部配置等。
總結:
Spring Boot 并不是對 Spring 功能上的增強,而是提供了一種快速使用 Spring 的方式
。
需求:
- 搭建 SpringBoot 工程,定義 HelloController.hello() 方法,傳回“Hello SpringBoot!”。
步驟:
- 建立 Maven 項目;
- 導入 SpringBoot 起步依賴:
<!--springboot工程需要繼承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!--web開發的起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 定義 Controller:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello() {
return "Hello SpringBoot!";
}
}
- 編寫引導類:
- controller 類必須是 Application 的所在包的類或者子包的類。
- 官網:程式隻加載 Application.java 所在包及其子包下的内容。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootQuickDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootQuickDemoApplication.class, args);
}
}
- 執行 main 方法,啟動測試:
小結:
- SpringBoot 在建立項目時,使用 jar 的打包方式。
- SpringBoot 的引導類,是項目入口,運作 main 方法就可以啟動項目。
- 使用 SpringBoot 和 Spring 建構的項目,業務代碼編寫方式完全一樣。
IDEA 快速建構 SpringBoot 工程:
<!--springboot工程需要繼承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!--web開發的起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 在 spring-boot-starter-parent 中定義了各種技術的版本資訊,組合了一套最優搭配的技術版本。
- 在各種 starter 中,定義了完成該功能需要的坐标合集,其中大部分版本資訊來自于父工程。
- 我們的工程繼承 parent,引入 starter 後,通過依賴傳遞,就可以簡單友善獲得需要的 jar 包,并且不會存在版本沖突等問題。
SpringBoot 是基于約定的,是以很多配置都有預設值,但如果想使用自己的配置替換預設配置的話,就可以使用 application.properties 或者 application.yml(application.yaml)進行配置。
# properties
server.port=8080
# yml
server:
port: 8080
- SpringBoot 提供了兩種配置檔案類型:properteis 和 yml/yaml
- 預設配置檔案名稱:application.properties(application 名稱固定)
- 在同一級目錄下優先級為:properties > yml > yaml
有關 YAML 的介紹與用法,可參考:《Java YAML 序列化》
- 方式一:@Value
- 方式二:Environment
- 方式三:@ConfigurationProperties
代碼示例:
- application.yml 配置檔案内容:
name: outer_name
person:
name: xiaoming
age: 18
address:
- shenzhen
- shanghai
- beijing
# 配置tomcat啟動端口
server:
port: 8082
- @ConfigurationProperties(對象注入):
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private String[] address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
}
- Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
// 方式一:@Value 注入
@Value("${name}")
private String name;
@Value("${person.name}")
private String personName;
@Value("${person.address[0]}")
private String personFirstAddress;
// 方式二:Environment 對象
@Autowired
private Environment env;
// 方式三:@ConfigurationProperties 對象注入
@Autowired
private Person person;
@RequestMapping("/hello")
public String sayHello() {
System.out.println(personName); // xiaoming
System.out.println(personFirstAddress); // shenzhen
System.out.println(env.getProperty("person.name")); // xiaoming
System.out.println(env.getProperty("person.address[0]")); // shenzhen
String[] address = person.getAddress();
for (String add: address) {
System.out.println(add);
}
/*
shenzhen
shanghai
beijing
*/
return "Get successfully!";
}
}
注意:以下提示不影響運作,在加了以下依賴後邊不再有此提示,且在編寫配置檔案時會有友好提示。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
我們在開發 SpringBoot 應用時,通常同一套程式會被安裝到不同環境,比如:開發、測試、生産等。其中資料庫位址、伺服器端口等等配置都不同,如果每次打包時,都要修改配置檔案的話,會非常麻煩。為此,profile 功能就是來進行動态配置切換的。
profile 的 2 種配置方式:
- 多 profile 檔案方式:提供多個配置檔案,每個代表一種環境
- application-dev.properties/yml (代表開發環境)
- application-test.properties/yml (代表測試環境)
- application-pro.properties/yml (代表生産環境)
- yml 多文檔方式:
- 在 yml 中使用“---”(必須三個橫杆)分隔不同配置
profile 的 3 種激活方式:
- 配置檔案:在配置檔案中配置 spring.profiles.active=dev
- 虛拟機參數:在 VM options 指定 -Dspring.profiles.active=dev
- 指令行參數:java –jar xxx.jar --spring.profiles.active=dev
- 多 profile 檔案方式:
- application.yml 多文檔方式配置&激活:
# 配置開發環境
server:
port: 8081
spring:
profiles: dev
---
# 配置測試環境
server:
port: 8082
spring:
profiles: test
---
# 配置生産環境
server:
port: 8083
spring:
profiles: pro
---
# 使用配置檔案激活profile
spring:
profiles:
active: pro
Springboot 程式啟動時,會從以下位置加載配置檔案:
- 目前項目下的 /config 目錄下(不會打進 jar 包)
- 目前項目的根目錄(不會打進 jar 包)
- classpath 的 /config 目錄下(classpath 即 resources 編譯後的目錄)
- classpath 的根目錄
注意:
- 以上的優先級從高到低,相同的配置項,高優會覆寫低優。
- 若不同的配置項,則會互補而不會覆寫。
通過官網檢視外部屬性加載順序:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
實作步驟:
- 搭建 SpringBoot 工程
- 引入 starter-test 起步依賴( IDEA 建構 SpringBoot 時自帶)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- 編寫測試類
- 添加測試相關注解
- Junit4
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes=main方法的啟動類.class)
- Junit5
- Junit4
- 編寫測試方法進行測試
- 引入 Redis 起步依賴(可使用 IDEA 快捷搭建與引入)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置 Redis 相關屬性(可選)
- 注入 RedisTemplate 模闆
- 測試類:
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 DemoApplicationTest {
@Autowired
private RedisTemplate redisTemplate; // 無配置情況下,預設連接配接本地redis(localhost:6379)
@Test
void testSet() {
// 存入資料
redisTemplate.boundValueOps("name").set("xiaoming");
}
@Test
void testGet() {
// 取資料
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
- (application)添加 Redis 配置:
- 引入 Mybatis 起步依賴、添加 Mysql 驅動
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- 編寫 DataSource 和 MyBatis 相關配置
- 定義表和實體類
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`password` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
insert into `t_user`(`id`,`username`,`password`) values (1,'zhangsan','123'),(2,'lisi','234');
- 編寫 dao 和 mapper 檔案/純注解開發
- 測試
- application.yml:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///world?serverTimeZone=UTC # 預設連接配接本地3306
username: root
password: admin
- UserMapper:
package com.example.mybatis_demo.mapper;
import com.example.mybatis_demo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
import com.example.mybatis_demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MybatisDemoApplicationTests {
@Autowired
UserMapper userMapper;
@Test
void testFindAll() {
userMapper.findAll().forEach(
user -> System.out.println(user)
);
}
}
- UserXmlMapper:
package com.example.mybatis_demo.mapper;
import com.example.mybatis_demo.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
# datasource
spring:
datasource:
url: jdbc:mysql:///springboot?serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml # mapper映射檔案路徑
type-aliases-package: com.itheima.springbootmybatis.domain
# config-location: # 指定mybatis的核心配置檔案
- UserMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis_demo.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user
</select>
</mapper>
import com.example.mybatis_demo.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MybatisDemoApplicationTests {
@Autowired
UserXmlMapper userMapper;
@Test
void testFindAll() {
userMapper.findAll().forEach(
user -> System.out.println(user)
);
}
}