劍指 Offer 55 - I. 二叉樹的深度
SpringBoot基礎
學習目標:
- 能夠理解Spring的優缺點
- 能夠理解SpringBoot的特點
- 能夠理解SpringBoot的核心功能
- 能夠搭建SpringBoot的環境
- 能夠完成application.properties配置檔案的配置
- 能夠完成application.yml配置檔案的配置
- 能夠使用SpringBoot整合Mybatis
- 能夠使用SpringBoot整合Junit
- 能夠使用SpringBoot整合SpringData JPA
一、SpringBoot簡介
1.1 原有Spring優缺點分析
1.1.1 Spring的優點分析
Spring是Java企業版(Java Enterprise Edition,JEE,也稱J2EE)的輕量級代替品。無需開發重量級的Enterprise JavaBean(EJB),Spring為企業級Java開發提供了一種相對簡單的方法,通過依賴注入和麵向切面程式設計,用簡單的Java物件(Plain Old Java Object,POJO)實現了EJB的功能。
1.1.2 Spring的缺點分析
雖然Spring的元件程式碼是輕量級的,但它的配置卻是重量級的。一開始,Spring用XML配置,而且是很多XML配置。Spring 2.5引入了基於註解的元件掃描,這消除了大量針對應用程式自身元件的顯式XML配置。Spring 3.0引入了基於Java的配置,這是一種型別安全的可重構配置方式,可以代替XML。
所有這些配置都代表了開發時的損耗。因為在思考Spring特性配置和解決業務問題之間需要進行思維切換,所以編寫配置擠佔了編寫應用程式邏輯的時間。和所有框架一樣,Spring實用,但與此同時它要求的回報也不少。
除此之外,專案的依賴管理也是一件耗時耗力的事情。在環境搭建時,需要分析要匯入哪些庫的座標,而且還需要分析匯入與之有依賴關係的其他庫的座標,一旦選錯了依賴的版本,隨之而來的不相容問題就會嚴重阻礙專案的開發進度。
1.2 SpringBoot的概述
1.2.1 SpringBoot解決上述Spring的缺點
SpringBoot對上述Spring的缺點進行的改善和優化,基於約定優於配置的思想,可以讓開發人員不必在配置與邏輯業務之間進行思維的切換,全身心的投入到邏輯業務的程式碼編寫中,從而大大提高了開發的效率,一定程度上縮短了專案週期。
1.2.2 SpringBoot的特點
- 為基於Spring的開發提供更快的入門體驗
- 開箱即用,沒有程式碼生成,也無需XML配置。同時也可以修改預設值來滿足特定的需求
- 提供了一些大型專案中常見的非功能性特性,如嵌入式伺服器、安全、指標,健康檢測、外部配置等
- SpringBoot不是對Spring功能上的增強,而是提供了一種快速使用Spring的方式
1.2.3 SpringBoot的核心功能
-
起步依賴
起步依賴本質上是一個Maven專案物件模型(Project Object Model,POM),定義了對其他庫的傳遞依賴,這些東西加在一起即支援某項功能。
簡單的說,起步依賴就是將具備某種功能的座標打包到一起,並提供一些預設的功能。
-
自動配置
Spring Boot的自動配置是一個執行時(更準確地說,是應用程式啟動時)的過程,考慮了眾多因素,才決定Spring配置應該用哪個,不該用哪個。該過程是Spring自動完成的。
注意:起步依賴和自動配置的原理剖析會在第三章《SpringBoot原理分析》進行詳細講解
二、SpringBoot快速入門
2.1 程式碼實現
2.1.1 建立Maven工程
使用idea工具建立一個maven工程,該工程為普通的java工程即可
2.1.2 新增SpringBoot的起步依賴
SpringBoot要求,專案要繼承SpringBoot的起步依賴spring-boot-starter-parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
SpringBoot要整合SpringMVC進行Controller的開發,所以專案要匯入web的啟動依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.1.3 編寫SpringBoot引導類
要通過SpringBoot提供的引導類起步SpringBoot才可以進行訪問
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
2.1.4 編寫Controller
在引導類MySpringBootApplication同級包或者子級包中建立QuickStartController
package com.itheima.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickStartController {
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 訪問成功!";
}
}
2.1.5 測試
執行SpringBoot起步類的主方法,控制檯列印日誌如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.1.RELEASE)
2018-05-08 14:29:59.714 INFO 5672 --- [ main] com.itheima.MySpringBootApplication : Starting MySpringBootApplication on DESKTOP-RRUNFUH with PID 5672 (C:\Users\muzimoo\IdeaProjects\IdeaTest\springboot_quick\target\classes started by muzimoo in C:\Users\muzimoo\IdeaProjects\IdeaTest)
... ... ...
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-08 14:30:03.126 INFO 5672 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-05-08 14:30:03.196 INFO 5672 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-08 14:30:03.206 INFO 5672 --- [ main] com.itheima.MySpringBootApplication : Started MySpringBootApplication in 4.252 seconds (JVM running for 5.583)
通過日誌發現,Tomcat started on port(s): 8080 (http) with context path ''
tomcat已經起步,埠監聽8080,web應用的虛擬工程名稱為空
開啟瀏覽器訪問url地址為:http://localhost:8080/quick
2.2 快速入門解析
2.2.2 SpringBoot程式碼解析
- @SpringBootApplication:標註SpringBoot的啟動類,該註解具備多種功能(後面詳細剖析)
- SpringApplication.run(MySpringBootApplication.class) 代表執行SpringBoot的啟動類,引數為SpringBoot啟動類的位元組碼物件
2.2.3 SpringBoot工程熱部署
我們在開發中反覆修改類、頁面等資源,每次修改後都是需要重新啟動才生效,這樣每次啟動都很麻煩,浪費了大量的時間,我們可以在修改程式碼後不重啟就能生效,在 pom.xml 中新增如下配置就可以實現這樣的功能,我們稱之為熱部署。
<!--熱部署配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
注意:IDEA進行SpringBoot熱部署失敗原因
出現這種情況,並不是熱部署配置問題,其根本原因是因為Intellij IEDA預設情況下不會自動編譯,需要對IDEA進行自動編譯的設定,如下:
然後 Shift+Ctrl+Alt+/,選擇Registry
2.2.4 使用idea快速建立SpringBoot專案
通過idea快速建立的SpringBoot專案的pom.xml中已經匯入了我們選擇的web的起步依賴的座標
<?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.itheima</groupId>
<artifactId>springboot_quick2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot_quick2</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>9</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>
可以使用快速入門的方式建立Controller進行訪問,此處不再贅述
三、SpringBoot原理分析
3.1 起步依賴原理分析
3.1.1 分析spring-boot-starter-parent
按住Ctrl點選pom.xml中的spring-boot-starter-parent,跳轉到了spring-boot-starter-parent的pom.xml,xml配置如下(只摘抄了部分重點配置):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
按住Ctrl點選pom.xml中的spring-boot-starter-dependencies,跳轉到了spring-boot-starter-dependencies的pom.xml,xml配置如下(只摘抄了部分重點配置):
<properties>
<activemq.version>5.15.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.63</appengine-sdk.version>
<artemis.version>2.4.0</artemis.version>
<aspectj.version>1.8.13</aspectj.version>
<assertj.version>3.9.1</assertj.version>
<atomikos.version>4.0.6</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
<byte-buddy.version>1.7.11</byte-buddy.version>
... ... ...
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
... ... ...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
</plugin>
... ... ...
</plugins>
</pluginManagement>
</build>
從上面的spring-boot-starter-dependencies的pom.xml中我們可以發現,一部分座標的版本、依賴管理、外掛管理已經定義好,所以我們的SpringBoot工程繼承spring-boot-starter-parent後已經具備版本鎖定等配置了。所以起步依賴的作用就是進行依賴的傳遞。
3.1.2 分析spring-boot-starter-web
按住Ctrl點選pom.xml中的spring-boot-starter-web,跳轉到了spring-boot-starter-web的pom.xml,xml配置如下(只摘抄了部分重點配置):
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
<name>Spring Boot Web Starter</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.1.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
從上面的spring-boot-starter-web的pom.xml中我們可以發現,spring-boot-starter-web就是將web開發要使用的spring-web、spring-webmvc等座標進行了“打包”,這樣我們的工程只要引入spring-boot-starter-web起步依賴的座標就可以進行web開發了,同樣體現了依賴傳遞的作用。
3.2 自動配置原理解析
按住Ctrl點選檢視啟動類MySpringBootApplication上的註解@SpringBootApplication
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
註解@SpringBootApplication的原始碼
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
... ... ...
}
其中,
@SpringBootConfiguration:等同與@Configuration,既標註該類是Spring的一個配置類
@EnableAutoConfiguration:SpringBoot自動配置功能開啟
按住Ctrl點選檢視註解@EnableAutoConfiguration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
... ... ...
}
其中,@Import(AutoConfigurationImportSelector.class) 匯入了AutoConfigurationImportSelector類
按住Ctrl點選檢視AutoConfigurationImportSelector原始碼
public String[] selectImports(AnnotationMetadata annotationMetadata) {
... ... ...
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return StringUtils.toStringArray(configurations);
}
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
return configurations;
}
其中,SpringFactoriesLoader.loadFactoryNames 方法的作用就是從META-INF/spring.factories檔案中讀取指定類對應的類名稱列表
spring.factories 檔案中有關自動配置的配置資訊如下:
... ... ...
org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
... ... ...
上面配置檔案存在大量的以Configuration為結尾的類名稱,這些類就是存有自動配置資訊的類,而SpringApplication在獲取這些類名後再載入
我們以ServletWebServerFactoryAutoConfiguration為例來分析原始碼:
@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {
... ... ...
}
其中,
@EnableConfigurationProperties(ServerProperties.class) 代表載入ServerProperties伺服器配置屬性類
進入ServerProperties.class原始碼如下:
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
/**
* Server HTTP port.
*/
private Integer port;
/**
* Network address to which the server should bind.
*/
private InetAddress address;
... ... ...
}
其中,
prefix = "server" 表示SpringBoot配置檔案中的字首,SpringBoot會將配置檔案中以server開始的屬性對映到該類的欄位中。對映關係如下:
四、SpringBoot的配置檔案
4.1 SpringBoot配置檔案型別
4.1.1 SpringBoot配置檔案型別和作用
SpringBoot是基於約定的,所以很多配置都有預設值,但如果想使用自己的配置替換預設配置的話,就可以使用application.properties或者application.yml(application.yaml)進行配置。
SpringBoot預設會從Resources目錄下載入application.properties或application.yml(application.yaml)檔案
其中,application.properties檔案是鍵值對型別的檔案,之前一直在使用,所以此處不在對properties檔案的格式進行闡述。除了properties檔案外,SpringBoot還可以使用yml檔案進行配置,下面對yml檔案進行講解。
4.1.2 application.yml配置檔案
4.1.2.1 yml配置檔案簡介
YML檔案格式是YAML (YAML Aint Markup Language)編寫的檔案格式,YAML是一種直觀的能夠被電腦識別的的資料資料序列化格式,並且容易被人類閱讀,容易和指令碼語言互動的,可以被支援YAML庫的不同的程式語言程式匯入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML檔案是以資料為核心的,比傳統的xml方式更加簡潔。
YML檔案的副檔名可以使用.yml或者.yaml。
4.1.2.2 yml配置檔案的語法
4.1.2.2.1 配置普通資料
-
語法: key: value
-
示例程式碼:
-
name: haohao
-
注意:value之前有一個空格
4.1.2.2.2 配置物件資料
-
語法:
key:
key1: value1
key2: value2
或者:
key: {key1: value1,key2: value2}
-
示例程式碼:
-
person: name: haohao age: 31 addr: beijing #或者 person: {name: haohao,age: 31,addr: beijing}
-
注意:key1前面的空格個數不限定,在yml語法中,相同縮排代表同一個級別
4.1.2.2.2 配置Map資料
同上面的物件寫法
4.1.2.2.3 配置陣列(List、Set)資料
-
語法:
key:
- value1
- value2
或者:
key: [value1,value2]
-
示例程式碼:
-
city: - beijing - tianjin - shanghai - chongqing #或者 city: [beijing,tianjin,shanghai,chongqing] #集合中的元素是物件形式 student: - name: zhangsan age: 18 score: 100 - name: lisi age: 28 score: 88 - name: wangwu age: 38 score: 90 #行內 student1: [{name: tom,age: 17},{name: tss,age: 23}] # map型別 #Map資料 Student: key1: value1 key2: value2
-
注意:value1與之間的 - 之間存在一個空格
4.1.3 SpringBoot配置資訊的查詢
上面提及過,SpringBoot的配置檔案,主要的目的就是對配置資訊進行修改的,但在配置時的key從哪裡去查詢呢?我們可以查閱SpringBoot的官方文件
常用的配置摘抄如下:
# QUARTZ SCHEDULER (QuartzProperties)
spring.quartz.jdbc.initialize-schema=embedded # Database schema initialization mode.
spring.quartz.jdbc.schema=classpath:org/quartz/impl/jdbcjobstore/tables_@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.quartz.job-store-type=memory # Quartz job store type.
spring.quartz.properties.*= # Additional Quartz Scheduler properties.
# ----------------------------------------
# WEB PROPERTIES
# ----------------------------------------
# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.port=8080 # Server HTTP port.
server.servlet.context-path= # Context path of the application.
server.servlet.path=/ # Path of the main dispatcher servlet.
# HTTP encoding (HttpEncodingProperties)
spring.http.encoding.charset=UTF-8 # Charset of HTTP requests and responses. Added to the "Content-Type" header if not set explicitly.
# JACKSON (JacksonProperties)
spring.jackson.date-format= # Date format string or a fully-qualified date format class name. For instance, `yyyy-MM-dd HH:mm:ss`.
# SPRING MVC (WebMvcProperties)
spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the dispatcher servlet.
spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
spring.datasource.password= # Login password of the database.
spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
# JEST (Elasticsearch HTTP client) (JestProperties)
spring.elasticsearch.jest.password= # Login password.
spring.elasticsearch.jest.proxy.host= # Proxy host the HTTP client should use.
spring.elasticsearch.jest.proxy.port= # Proxy port the HTTP client should use.
spring.elasticsearch.jest.read-timeout=3s # Read timeout.
spring.elasticsearch.jest.username= # Login username.
我們可以通過配置application.poperties 或者 application.yml 來修改SpringBoot的預設配置
例如:
application.properties檔案
server.port=8888
server.servlet.context-path=demo
application.yml檔案
server:
port: 8888
servlet:
context-path: /demo
4.2 配置檔案與配置類的屬性對映方式
4.2.1 使用註解@Value對映
我們可以通過@Value註解將配置檔案中的值對映到一個Spring管理的Bean的欄位上
例如:
application.properties配置如下:
person:
name: zhangsan
age: 18
或者,application.yml配置如下:
person:
name: zhangsan
age: 18
實體Bean程式碼如下:
@Controller
public class QuickStartController {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private Integer age;
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 訪問成功! name="+name+",age="+age;
}
}
瀏覽器訪問地址:http://localhost:8080/quick 結果如下:
4.2.2 使用註解@ConfigurationProperties對映
通過註解@ConfigurationProperties(prefix="配置檔案中的key的字首")可以將配置檔案中的配置自動與實體進行對映
application.properties配置如下:
注意需要提供get和set方法
person:
name: zhangsan
age: 18
或者,application.yml配置如下:
person:
name: zhangsan
age: 18
實體Bean程式碼如下:
@Controller
@ConfigurationProperties(prefix = "person")
public class QuickStartController {
private String name;
private Integer age;
@RequestMapping("/quick")
@ResponseBody
public String quick(){
return "springboot 訪問成功! name="+name+",age="+age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
}
瀏覽器訪問地址:http://localhost:8080/quick 結果如下:
注意:使用@ConfigurationProperties方式可以進行配置檔案與實體欄位的自動對映,但需要欄位必須提供set方法才可以,而使用@Value註解修飾的欄位不需要提供set方法
SpringBoot的configuration-processor配置檔案
您可以@ConfigurationProperties
使用spring-boot-configuration-processor
jar 從包含註釋的專案中輕鬆生成自己的配置元資料檔案 。該jar包含一個Java註釋處理器,在您的專案被編譯時會被呼叫。要使用處理器,請包含對的依賴 spring-boot-configuration-processor
。
<!--對映@ConfiguaritionProperties執行器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
五、SpringBoot與整合其他技術
5.1 SpringBoot整合Mybatis
5.1.1 新增Mybatis的起步依賴
<!--mybatis起步依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
5.1.2 新增資料庫驅動座標
<!-- MySQL連線驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5.1.3 新增資料庫連線資訊
在application.properties中新增資料量的連線資訊
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
5.1.4 建立user表
在test資料庫中建立user表
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '張三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
5.1.5 建立實體Bean
public class User {
// 主鍵
private Long id;
// 使用者名稱
private String username;
// 密碼
private String password;
// 姓名
private String name;
//此處省略getter和setter方法 .. ..
}
5.1.6 編寫Mapper
@Mapper
public interface UserMapper {
public List<User> queryUserList();
}
注意:@Mapper標記該類是一個mybatis的mapper介面,可以被spring boot自動掃描到spring上下文中
5.1.7 配置Mapper對映檔案
在src\main\resources\mapper路徑下加入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.itheima.mapper.UserMapper">
<select id="queryUserList" resultType="user">
select * from user
</select>
</mapper>
5.1.8 在application.properties中新增mybatis的資訊
#spring整合Mybatis環境
#pojo別名掃描包
mybatis.type-aliases-package=com.itheima.domain
#載入Mybatis對映檔案
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
5.1.9 編寫測試Controller
@Controller
public class MapperController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUser")
@ResponseBody
public List<User> queryUser(){
List<User> users = userMapper.queryUserList();
return users;
}
}
5.1.10 測試
5.2 SpringBoot整合Junit
5.2.1 新增Junit的起步依賴
<!--測試的起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
5.2.2 編寫測試類
package com.itheima.test;
import com.itheima.MySpringBootApplication;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class MapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void test() {
List<User> users = userMapper.queryUserList();
System.out.println(users);
}
}
其中,
SpringRunner繼承自SpringJUnit4ClassRunner,使用哪一個Spring提供的測試測試引擎都可以
public final class SpringRunner extends SpringJUnit4ClassRunner
@SpringBootTest的屬性指定的是引導類的位元組碼物件
5.2.3 控制檯列印資訊
5.3 SpringBoot整合Spring Data JPA
5.3.1 新增Spring Data JPA的起步依賴
<!-- springBoot JPA的起步依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
5.3.2 新增資料庫驅動依賴
<!-- MySQL連線驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5.3.3 在application.properties中配置資料庫和jpa的相關屬性
#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#JPA Configuration:
spring.jpa.database=MySQL
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
5.3.4 建立實體配置實體
@Entity
public class User {
// 主鍵
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// 使用者名稱
private String username;
// 密碼
private String password;
// 姓名
private String name;
//此處省略setter和getter方法... ...
}
5.3.5 編寫UserRepository
public interface UserRepository extends JpaRepository<User,Long>{
public List<User> findAll();
}
5.3.6 編寫測試類
@RunWith(SpringRunner.class)
@SpringBootTest(classes=MySpringBootApplication.class)
public class JpaTest {
@Autowired
private UserRepository userRepository;
@Test
public void test(){
List<User> users = userRepository.findAll();
System.out.println(users);
}
}
5.3.7 控制檯列印資訊
注意:如果是jdk9,執行報錯如下:
原因:jdk缺少相應的jar
解決方案:手動匯入對應的maven座標,如下:
<!--jdk9需要匯入如下座標-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
5.4 SpringBoot整合Redis
5.4.1 新增redis的起步依賴
<!-- 配置使用redis啟動器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
5.4.2 配置redis的連線資訊
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
5.4.3 注入RedisTemplate測試redis操作
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJpaApplication.class)
public class RedisTest {
@Autowired
private UserRepository userRepository;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void test() throws JsonProcessingException {
//從redis快取中獲得指定的資料
String userListData = redisTemplate.boundValueOps("user.findAll").get();
//如果redis中沒有資料的話
if(null==userListData){
//查詢資料庫獲得資料
List<User> all = userRepository.findAll();
//轉換成json格式字串
ObjectMapper om = new ObjectMapper();
userListData = om.writeValueAsString(all);
//將資料儲存到redis中,下次在查詢直接從redis中獲得資料,不用在查詢資料庫
redisTemplate.boundValueOps("user.findAll").set(userListData);
System.out.println("===============從資料庫獲得資料===============");
}else{
System.out.println("===============從redis快取中獲得資料===============");
}
System.out.println(userListData);
}
}