日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot 中 4 大核心组件,你了解多少?

發布時間:2025/3/11 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot 中 4 大核心组件,你了解多少? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring Boot 中的 4 大組件分別是:Spring Boot Starter、Spring Boot Autoconfigure、Spring Boot CLI 以及 Spring Boot actuator,接下來,我們分別來看他們的使用和作用。

1.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。

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: trueservlet:content-type: text/htmlmode: 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"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>

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置整合mybatis過程 --><!-- 1.配置數據庫相關參數properties的屬性:${url} --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 2.數據庫連接池 --><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" /><!-- 當獲取連接失敗重試次數 --><property name="acquireRetryAttempts" value="2" /></bean><!-- 3.配置SqlSessionFactory對象 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入數據庫連接池 --><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接口包,動態實現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>

1.2.3 小結

a、Starter 幫我們封裝好了所有需要的依賴,避免我們自己添加導致的一些Jar包沖突或者缺少包的情況;

b、Starter幫我們自動注入了需要的Bean實例到Spring 容器中,不需要我們手動配置(這個可以說是starter干的,實際上并不是,這里埋個坑,下面解答);

所以: starter包的內容就是pom文件,就是一個依賴傳遞包。

2.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體現出來的能力的代碼實現

3.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腳本了。

4.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 添加依賴

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId> </dependency>

4.2 配置需要開啟監控的端點

management:endpoint:health: ## 開啟健康監控端點enabled: truebeans: ## 開啟Bean實例監控端點enabled: true

4.3 啟動服務并驗證

4.3.1 啟動結果

圖片

4.3.2 查看各個監控信息

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

圖片

查看服務健康狀態:

圖片

其他API查看官方文檔了解了。

總結

本章主要介紹了Spring Boot的四大組件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,需要深入了解的小伙伴可以查看官方的API文檔。

上期送書活動中獎名單

恭喜以下三位:

上期中獎規則為:第 6 名、第 16 名、第 26 名留言的用戶,因為總留言數只有 20 條,所以把第 26 名中獎的用戶直接改為最新留言的用戶,恭喜以上三位。請以上中獎的用戶加磊哥的微信號:GG_Stone 發送中獎信息和郵寄地址。其他小伙伴下次也積極參與哦,說不定下個中獎的人就是你呢?

往期推薦

SpringBoot時間格式化的5種方法!


SpringBoot 如何統一后端返回格式?老鳥們都是這樣玩的!


SpringBoot 優雅的參數效驗!

總結

以上是生活随笔為你收集整理的SpringBoot 中 4 大核心组件,你了解多少?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。