<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot 四大核心組件,你知道幾個(gè)?

          共 1665字,需瀏覽 4分鐘

           ·

          2022-03-10 15:14

          ????關(guān)注后回復(fù)?“進(jìn)群”?,拉你進(jìn)程序員交流群????

          來源:blog.csdn.net/u011909918/article/details/109647196

          前言

          先透露一下,四大組件分別是:starter, autoconfigure, CLI 以及actuator。

          下面我們就來詳細(xì)介紹一些他們有什么用。

          一、Spring Boot Starter

          1.1 Starter的應(yīng)用示例


          ????org.springframework.boot
          ????spring-boot-starter-thymeleaf



          ????org.mybatis.spring.boot
          ????mybatis-spring-boot-starter
          ????1.3.2

          Spring Boot 基礎(chǔ)就不介紹了,推薦下這個(gè)實(shí)戰(zhàn)教程:

          https://github.com/javastacks/spring-boot-best-practice

          在我們的Spring Boot項(xiàng)目種的POM文件中總會(huì)看到這兩種依賴:

          spring-boot-starter-xxxxxx-spring-boot-starter

          這就是spring boot的四大組件之一的starter。

          a、spring-boot-starter-thymeleaf

          b、mybatis-spring-boot-starter

          兩種starter的區(qū)別就是 >>

          其中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??#注意:一定要對(duì)應(yīng)mapper映射xml文件的所在路徑
          ??type-aliases-package:?com.hi.ld.vo.system??#?注意:對(duì)應(yīng)實(shí)體類的路徑
          ??configuration:
          ????log-impl:?org.apache.ibatis.logging.stdout.StdOutImpl

          下面讓我們來看看以前怎么配置thymeleaf。

          1.2 Spring Boot之前的Thymeleaf和Mybatis應(yīng)用

          廢話不多說,直接上代碼:

          1.2.1 Thymeleaf配置

          a. 添加對(duì)應(yīng)依賴:


          ??org.thymeleaf
          ??thymeleaf-spring5
          ??3.0.11.RELEASE


          ??org.thymeleaf.extras
          ??thymeleaf-extras-java8time
          ??3.0.4.RELEASE

          b. bean配置

          "templateResolver"
          ???????class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
          ??"prefix"?value="/WEB-INF/templates/"?/>
          ??"suffix"?value=".html"?/>
          ??"templateMode"?value="HTML5"?/>


          "templateEngine"
          ??????class="org.thymeleaf.spring4.SpringTemplateEngine">
          ??"templateResolver"?ref="templateResolver"?/>


          "org.thymeleaf.spring4.view.ThymeleafViewResolver">
          ??"templateEngine"?ref="templateEngine"?/>

          1.2.2 Mybatis配置

          a. 添加對(duì)應(yīng)依賴:

          ?
          ???org.springframework.boot
          ???spring-boot-starter-jdbc
          ?

          ?
          ???org.mybatis
          ???mybatis
          ?

          ?
          ???org.mybatis
          ???mybatis-spring
          ?

          b. bean配置

          下面的第3, 4步驟就是Mybatis相關(guān)配置。第一步是引入資源配置。第二步是配置數(shù)據(jù)源

          "1.0"?encoding="UTF-8"?>
          "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"
          >
          ?
          ?
          ?"classpath:jdbc.properties"?/>

          ?
          ?"dataSource"?class="com.mchange.v2.c3p0.ComboPooledDataSource">
          ??
          ??"driverClass"?value="${jdbc.driver}"?/>
          ??"jdbcUrl"?value="${jdbc.url}"?/>
          ??"user"?value="${jdbc.username}"?/>
          ??"password"?value="${jdbc.password}"?/>

          ??
          ??"maxPoolSize"?value="30"?/>
          ??"minPoolSize"?value="10"?/>
          ??
          ??"autoCommitOnClose"?value="false"?/>
          ??
          ??"checkoutTimeout"?value="10000"?/>
          ??
          ??"acquireRetryAttempts"?value="2"?/>
          ?

          ?
          ?"sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">
          ??
          ??"dataSource"?ref="dataSource"?/>
          ??
          ??"configLocation"?value="classpath:mybatis-config.xml"?/>
          ??
          ??"typeAliasesPackage"?value="com.soecode.lyf.entity"?/>
          ??
          ??"mapperLocations"?value="classpath:mapper/*.xml"?/>
          ?

          ?
          ?"org.mybatis.spring.mapper.MapperScannerConfigurer">
          ??
          ??"sqlSessionFactoryBeanName"?value="sqlSessionFactory"?/>
          ??
          ??"basePackage"?value="com.soecode.lyf.dao"?/>
          ?

          1.2.3 小結(jié)

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

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

          所以: starter包的內(nèi)容就是pom文件,就是一個(gè)依賴傳遞包。


          二、Spring Boot Autoconfigure

          2.1 autoconfigure 簡介

          autoconfigure在我們的開發(fā)中并不會(huì)被感知,因?yàn)樗谴嬖谂c我們的starter中的。所以我們的每個(gè)starter都是依賴autoconfigure的:

          當(dāng)然我們也可以把a(bǔ)utoconfig的內(nèi)容直接放在starter包里邊。

          分享資料:Spring Boot 學(xué)習(xí)筆記,這個(gè)太全了!

          a. spring-boot-autoconfigure:

          注意:這里有個(gè)點(diǎn),就是官網(wǎng)提供的configure大多數(shù)在spring-boot-autoconfigure包里邊,并沒有單獨(dú)創(chuàng)建新包。

          b、mybatis-spring-boot-autoconfigure

          2.2 小結(jié)

          autoconfigure內(nèi)容是配置Bean實(shí)例到Spring容器的實(shí)際代碼實(shí)現(xiàn)包,然后提供給starter依賴。所以說1.2.3中的b項(xiàng)所說的配置Bean實(shí)例到Spring容器中實(shí)際是autoconfigure做的,因?yàn)槭莝tarter依賴它,所以也可以說是starter干的。

          所以:autocinfigure是starter體現(xiàn)出來的能力的代碼實(shí)現(xiàn)

          三、Spring Boot CLI

          Spring Boot CLI是一個(gè)命令行使用Spring Boot的客戶端工具;主要功能如下:

          • 運(yùn)行g(shù)roovy腳本 => 官網(wǎng)2.1
          • 打包groovy文件到j(luò)ar => 官網(wǎng)2.3
          • 初始化Spring Boot項(xiàng)目 => 官網(wǎng)2.4
          • 其他

          先上個(gè)官網(wǎng)文檔:

          https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-cli.html

          因?yàn)檫@個(gè)我們用的比較少,所以就不多贅述了。個(gè)人感覺比較流脾的功能就是命令行直接執(zhí)行g(shù)roovy腳本了。

          四、Spring Boot actuator

          actuator是Spring Boot的監(jiān)控插件,本身提供了很多接口可以獲取當(dāng)前項(xiàng)目的各項(xiàng)運(yùn)行狀態(tài)指標(biāo)。Spring Boot 學(xué)習(xí)筆記,這個(gè)分享給你。

          官網(wǎng)文檔:

          https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/production-ready-features.html#production-ready

          名詞解釋:

          Endpoints: 需要監(jiān)控的端點(diǎn)。參考官網(wǎng)第二節(jié)官網(wǎng)文檔

          可用的端點(diǎn):

          下方的是web工程的端點(diǎn)。

          使用方法如下:

          4.1 添加依賴

          Spring Boot 基礎(chǔ)就不介紹了,推薦下這個(gè)實(shí)戰(zhàn)教程:

          https://github.com/javastacks/spring-boot-best-practice


          ????org.springframework.boot
          ????spring-boot-starter-actuator

          4.2 配置需要開啟監(jiān)控的端點(diǎn)

          management:
          ??endpoint:
          ????health:?##?開啟健康監(jiān)控端點(diǎn)
          ??????enabled:?true
          ????beans:?##?開啟Bean實(shí)例監(jiān)控端點(diǎn)
          ??????enabled:?true

          4.3 啟動(dòng)服務(wù)并驗(yàn)證

          4.3.1 啟動(dòng)結(jié)果

          4.3.2 查看各個(gè)監(jiān)控信息

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

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

          其他API查看官方文檔了解或者留言一起研究一下,厚著臉皮我也沒怎么用過這個(gè)。不過下一章介紹了starter和autoconfigure之后我們就可以去研究actuator的源碼了。。。。

          總結(jié)

          本章主要介紹了Spring Boot的四大組件的作用,其中主要是starter和autoconfigure,另外的CLI和actuator用的并不多,所以沒有仔細(xì)介紹。

          -End-

          最近有一些小伙伴,讓我?guī)兔φ乙恍?面試題?資料,于是我翻遍了收藏的 5T 資料后,匯總整理出來,可以說是程序員面試必備!所有資料都整理到網(wǎng)盤了,歡迎下載!

          點(diǎn)擊??卡片,關(guān)注后回復(fù)【面試題】即可獲取

          在看點(diǎn)這里好文分享給更多人↓↓

          瀏覽 90
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成年人的免费视频 | 日韩免费一级 | 韩国一区视频在线观看 | 久久久青青草 | 精品久久久久久中文字幕无码专区 |