來源:blog.csdn.net/u011909918/article/details/109647196
前言
先透露一下,四大組件分別是: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 基礎就不介紹了,推薦下這個實戰教程:
https://github.com/javastacks/spring-boot-best-practice
在我們的Spring Boot專案種的POM檔案中總會看到這兩種依賴:
spring-boot-starter-xxx 和 xxx-spring-boot-starter,
這就是spring boot的四大組件之一的starter,
a、spring-boot-starter-thymeleaf

b、mybatis-spring-boot-starter

兩種starter的區別就是 >>
- 官方提供的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,
1.2 Spring Boot之前的Thymeleaf和Mybatis應用
廢話不多說,直接上代碼:
1.2.1 Thymeleaf配置
a. 添加對應依賴:
<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>
b. bean配置
<bean id="templateResolver"
>
<property name="prefix" value="https://www.cnblogs.com/WEB-INF/templates/" />
<property name="suffix" value="https://www.cnblogs.com/javastack/p/.html" />
<property name="templateMode" value="https://www.cnblogs.com/javastack/p/HTML5" />
</bean>
<bean id="templateEngine"
>
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean >
<property name="templateEngine" ref="templateEngine" />
</bean>
1.2.2 Mybatis配置
a. 添加對應依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
b. bean配置
下面的第3, 4步驟就是Mybatis相關配置,第一步是引入資源配置,第二步是配置資料源
<?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.配置資料庫相關引數properties的屬性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 2.資料庫連接池 -->
<bean id="dataSource" >
<!-- 配置連接池屬性 -->
<property name="driverClass" value="https://www.cnblogs.com/javastack/p/${jdbc.driver}" />
<property name="jdbcUrl" value="https://www.cnblogs.com/javastack/p/${jdbc.url}" />
<property name="user" value="https://www.cnblogs.com/javastack/p/${jdbc.username}" />
<property name="password" value="https://www.cnblogs.com/javastack/p/${jdbc.password}" />
<!-- c3p0連接池的私有屬性 -->
<property name="maxPoolSize" value="https://www.cnblogs.com/javastack/p/30" />
<property name="minPoolSize" value="https://www.cnblogs.com/javastack/p/10" />
<!-- 關閉連接后不自動commit -->
<property name="autoCommitOnClose" value="https://www.cnblogs.com/javastack/p/false" />
<!-- 獲取連接超時時間 -->
<property name="checkoutTimeout" value="https://www.cnblogs.com/javastack/p/10000" />
<!-- 當獲取連接失敗重試次數 -->
<property name="acquireRetryAttempts" value="https://www.cnblogs.com/javastack/p/2" />
</bean>
<!-- 3.配置SqlSessionFactory物件 -->
<bean id="sqlSessionFactory" >
<!-- 注入資料庫連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全域組態檔:mybatis-config.xml -->
<property name="configLocation" value="https://www.cnblogs.com/javastack/p/classpath:mybatis-config.xml" />
<!-- 掃描entity包 使用別名 -->
<property name="typeAliasesPackage" value="https://www.cnblogs.com/javastack/p/com.soecode.lyf.entity" />
<!-- 掃描sql組態檔:mapper需要的xml檔案 -->
<property name="mapperLocations" value="https://www.cnblogs.com/javastack/p/classpath:mapper/*.xml" />
</bean>
<!-- 4.配置掃描Dao介面包,動態實作Dao介面,注入到spring容器中 -->
<bean >
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="https://www.cnblogs.com/javastack/p/sqlSessionFactory" />
<!-- 給出需要掃描Dao介面包 -->
<property name="basePackage" value="https://www.cnblogs.com/javastack/p/com.soecode.lyf.dao" />
</bean>
</beans>
1.2.3 小結
a、Starter 幫我們封裝好了所有需要的依賴,避免我們自己添加導致的一些Jar包沖突或者缺少包的情況;
b、Starter幫我們自動注入了需要的Bean實體到Spring 容器中,不需要我們手動配置(這個可以說是starter干的,實際上并不是,這里埋個坑,下面解答);
所以: starter包的內容就是pom檔案,就是一個依賴傳遞包,
二、Spring Boot Autoconfigure
2.1 autoconfigure 簡介
autoconfigure在我們的開發中并不會被感知,因為它是存在與我們的starter中的,所以我們的每個starter都是依賴autoconfigure的:

當然我們也可以把autoconfig的內容直接放在starter包里邊,
a. spring-boot-autoconfigure:
注意:這里有個點,就是官網提供的configure大多數在spring-boot-autoconfigure包里邊,并沒有單獨創建新包,

b、mybatis-spring-boot-autoconfigure

2.2 小結
autoconfigure內容是配置Bean實體到Spring容器的實際代碼實作包,然后提供給starter依賴,所以說1.2.3中的b項所說的配置Bean實體到Spring容器中實際是autoconfigure做的,因為是starter依賴它,所以也可以說是starter干的,
所以:autocinfigure是starter體現出來的能力的代碼實作
三、Spring Boot CLI
Spring Boot CLI是一個命令列使用Spring Boot的客戶端工具;主要功能如下:
- 運行groovy腳本 => 官網2.1
- 打包groovy檔案到jar => 官網2.3
- 初始化Spring Boot專案 => 官網2.4
- 其他
先上個官網檔案:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html
因為這個我們用的比較少,所以就不多贅述了,個人感覺比較流脾的功能就是命令列直接執行groovy腳本了,
四、Spring Boot actuator
actuator是Spring Boot的監控插件,本身提供了很多介面可以獲取當前專案的各項運行狀態指標,
官網檔案:
https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/production-ready-features.html#production-ready
名詞解釋:
Endpoints: 需要監控的端點,參考官網第二節官網檔案
可用的端點:


下方的是web工程的端點,
使用方法如下:
4.1 添加依賴
Spring Boot 基礎就不介紹了,推薦下這個實戰教程:
https://github.com/javastacks/spring-boot-best-practice
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.2 配置需要開啟監控的端點
management:
endpoint:
health: ## 開啟健康監控端點
enabled: true
beans: ## 開啟Bean實體監控端點
enabled: true
4.3 啟動服務并驗證
4.3.1 啟動結果

4.3.2 查看各個監控資訊
瀏覽器訪問(查看監控資訊地址):http://localhost:9500/actuator

查看服務健康狀態:

其他API查看官方檔案了解或者留言一起研究一下,厚著臉皮我也沒怎么用過這個,不過下一章介紹了starter和autoconfigure之后我們就可以去研究actuator的原始碼了,,,,
總結
本章主要介紹了Spring Boot的四大組件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,所以沒有仔細介紹,
近期熱文推薦:
1.1,000+ 道 Java面試題及答案整理(2022最新版)
2.勁爆!Java 協程要來了,,,
3.Spring Boot 2.x 教程,太全了!
4.20w 程式員紅包封面,快快領取,,,
5.《Java開發手冊(嵩山版)》最新發布,速速下載!
覺得不錯,別忘了隨手點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/431412.html
標籤:Java
