面試官:SpringBoot四大核心組件,你知道幾個?
你知道的越多,不知道的就越多,業(yè)余的像一棵小草!
你來,我們一起精進!你不來,我和你的競爭對手一起精進!
編輯:業(yè)余草
blog.csdn.net/u011909918
推薦:https://www.xttblog.com/?p=5261
前言
先透露一下,四大組件分別是:starter, autoconfigure, CLI 以及actuator。下面我們就來詳細介紹一些他們有什么用。
一、Spring Boot Starter
1.1 Starter的應用示例
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
在我們的 Spring Boot 項目種的 POM 文件中總會看到這兩種依賴:
spring-boot-starter-xxx 和 xxx-spring-boot-starter。
這就是 spring boot 的四大組件之一的 starter。
「spring-boot-starter-thymeleaf」

在這里插入圖片描述
「mybatis-spring-boot-starter」

在這里插入圖片描述
「兩種 starter 的區(qū)別就是 >>」
官方提供的 starter 是這樣的:spring-boot-starter-xxx 非官方的 starter 是這樣的:xxx-spring-boot-starter
其中 xxx 就是我們想要依賴的組件或者 jar 包。上例就是我們 spring boot 用來引入 thymeleaf 引擎和 mybatis 框架所配置的依賴。引入之后通過簡單的約定配置就可以正常使用。比如:
Thymeleaf 引擎約定配置:
##前端引擎配置
spring:
thymeleaf:
enabled: true
servlet:
content-type: text/html
mode: HTML
## 頁面前綴
prefix: classpath:/templates/
## 后綴
suffix: .html
Mybatis 約定配置:
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要對應mapper映射xml文件的所在路徑
type-aliases-package: com.hi.ld.vo.system # 注意:對應實體類的路徑
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
下面讓我們來看看以前怎么配置 thymeleaf。
Spring Boot 之前的 Thymeleaf 和 Mybatis 應用
廢話不多說,直接上代碼:
Thymeleaf 配置
「添加對應依賴:」
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
「bean配置」
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
Mybatis 配置
「添加對應依賴:」
```xml
**bean配置**
下面的第 3, 4 步驟就是 Mybatis 相關配置。第一步是引入資源配置。第二步是配置數(shù)據(jù)源
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置整合mybatis過程 -->
<!-- 1.配置數(shù)據(jù)庫相關參數(shù)properties的屬性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2.數(shù)據(jù)庫連接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 配置連接池屬性 -->
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- c3p0連接池的私有屬性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 關閉連接后不自動commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 獲取連接超時時間 -->
<property name="checkoutTimeout" value="10000" />
<!-- 當獲取連接失敗重試次數(shù) -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!-- 3.配置SqlSessionFactory對象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入數(shù)據(jù)庫連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 掃描entity包 使用別名 -->
<property name="typeAliasesPackage" value="com.soecode.lyf.entity" />
<!-- 掃描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!-- 4.配置掃描Dao接口包,動態(tài)實現(xiàn)Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 給出需要掃描Dao接口包 -->
<property name="basePackage" value="com.soecode.lyf.dao" />
</bean>
</beans>
小結
a、Starter 幫我們封裝好了所有需要的依賴,避免我們自己添加導致的一些Jar包沖突或者缺少包的情況;
b、Starter 幫我們自動注入了需要的 Bean 實例到 Spring 容器中,不需要我們手動配置(這個可以說是 starter 干的,實際上并不是,這里埋個坑,下面解答);
所以: starter 包的內(nèi)容就是 pom 文件,就是一個依賴傳遞包。
Spring Boot Autoconfigure
autoconfigure 簡介
autoconfigure 在我們的開發(fā)中并不會被感知,因為它是存在與我們的 starter 中的。所以我們的每個 starter 都是依賴 autoconfigure 的:

在這里插入圖片描述
當然我們也可以把 autoconfig 的內(nèi)容直接放在 starter 包里邊。
「spring-boot-autoconfigure」
注意:這里有個點,就是官網(wǎng)提供的 configure 大多數(shù)在 spring-boot-autoconfigure 包里邊,并沒有單獨創(chuàng)建新包。

在這里插入圖片描述
「mybatis-spring-boot-autoconfigure」

在這里插入圖片描述
小結
autoconfigure 內(nèi)容是配置 Bean 實例到 Spring 容器的實際代碼實現(xiàn)包,然后提供給 starter 依賴。所以說 1.2.3 中的 b 項所說的配置 Bean 實例到Sprin g容器中實際是 autoconfigure 做的,因為是 starter 依賴它,所以也可以說是 starter 干的。
所以:autocinfigure 是 starter 體現(xiàn)出來的能力的代碼實現(xiàn)
Spring Boot CLI
Spring Boot CLI 是一個命令行使用 Spring Boot 的客戶端工具;主要功能如下:
運行 groovy 腳本 => 官網(wǎng) 2.1 打包 groovy 文件到 jar => 官網(wǎng) 2.3 初始化 Spring Boot 項目 => 官網(wǎng) 2.4 其他
先上個官網(wǎng)文檔:
?https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html
?
因為這個我們用的比較少,所以就不多贅述了。個人感覺比較流脾的功能就是命令行直接執(zhí)行 groovy 腳本了。
Spring Boot actuator
actuator 是 Spring Boot 的監(jiān)控插件,本身提供了很多接口可以獲取當前項目的各項運行狀態(tài)指標。
官網(wǎng)文檔:
?https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/production-ready-features.html#production-ready
?
名詞解釋:
Endpoints: 需要監(jiān)控的端點。參考官網(wǎng)第二節(jié)官網(wǎng)文檔
「可用的端點」


下方的是 web 工程的端點。
使用方法如下:
添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置需要開啟監(jiān)控的端點
management:
endpoint:
health: ## 開啟健康監(jiān)控端點
enabled: true
beans: ## 開啟Bean實例監(jiān)控端點
enabled: true
啟動服務并驗證
啟動結果

查看各個監(jiān)控信息
瀏覽器訪問(查看監(jiān)控信息地址):http://localhost:9500/actuator

查看服務健康狀態(tài):

其他 API 查看官方文檔了解或者留言一起研究一下,厚著臉皮我也沒怎么用過這個。不過下一章介紹了 starter 和 autoconfigure 之后我們就可以去研究 actuator 的源碼了。。。。
總結
本章主要介紹了 Spring Boot 的四大組件的作用,其中主要是 starter 和autoconfigure,另外的 CLI 和 actuator 用的并不多,所以沒有仔細介紹。
文章有幫助的話,在看,轉(zhuǎn)發(fā)吧。謝謝支持喲 (*^__^*)
