javascript
Spring Boot - 手把手教小师妹自定义Spring Boot Starter
文章目錄
- Pre
- 自定義starter的套路
- 步驟
- 命名規(guī)范
- 官方命名空間
- 自定義命名空間
- 實(shí)戰(zhàn)
- 創(chuàng)建一個(gè)父maven項(xiàng)目:springboot_custome_starter
- 創(chuàng)建 兩個(gè)Module: artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer
- artisan-spring-boot-starter (空的jar文件,僅僅提供輔助性依賴管理)
- pom
- artisan-spring-boot-starter-autoconfigurer
- pom
- CustomProperties
- IndexController
- CustomAutoConfiguration 自動(dòng)配置類
- spring.factories
- 打包發(fā)布
- 引用自定義starter測試
Pre
SpringBoot 最強(qiáng)大的功能就是把我們常用的場景抽取成了一個(gè)個(gè)starter(場景啟動(dòng)器),我們通過引入springboot 為我提供的這些場景啟動(dòng)器,再進(jìn)行少量的配置就能使用相應(yīng)的功能。
但有些時(shí)候,springboot也不能囊括我們所有的使用場景,往往我們需要自定義starter,來簡化我們對(duì)springboot的使用。
那怎么搞呢?
自定義starter的套路
參照@WebMvcAutoConfiguration , 看看們需要準(zhǔn)備哪些東西
@Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }) @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10) @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class }) public class WebMvcAutoConfiguration { ....... }抽取以下
@Configuration //指定這個(gè)類是一個(gè)配置類 @ConditionalOnXXX //指定條件成立的情況下自動(dòng)配置類生效 @AutoConfigureOrder //指定自動(dòng)配置類的順序 @Bean //向容器中添加組件 @ConfigurationProperties //結(jié)合相關(guān)xxxProperties來綁定相關(guān)的配置 @EnableConfigurationProperties //讓xxxProperties生效加入到容器中自動(dòng)配置類要能加載需要將自動(dòng)配置類,配置在META-INF/spring.factories中
我們參考下 spring-boot-starter
點(diǎn)擊maven進(jìn)去
有個(gè) spring-boot-autoconfigure的依賴
步驟
所以總結(jié)下
- 啟動(dòng)器(starter)是一個(gè)空的jar文件,僅僅提供輔助性依賴管理,這些依賴可能用于自動(dòng)裝配或其他類庫。
- 需要專門寫一個(gè)類似spring-boot-autoconfigure的配置模塊
- 用的時(shí)候只需要引入啟動(dòng)器starter,就可以使用自動(dòng)配置了
命名規(guī)范
官方命名空間
前綴:spring-boot-starter-
模式:spring-boot-starter-模塊名
舉例:spring-boot-starter-web、spring-boot-starter-jdbc
自定義命名空間
后綴:-spring-boot-starter
模式:模塊-spring-boot-starter
舉例:mybatis-spring-boot-starter
實(shí)戰(zhàn)
創(chuàng)建一個(gè)父maven項(xiàng)目:springboot_custome_starter
新建一個(gè)maven工程, 父工程,pom類型 , src 目錄 和 IDEA自動(dòng)創(chuàng)建的.md文件,刪除掉
pom 如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><packaging>pom</packaging><groupId>com.artisan.customstarter</groupId><artifactId>starter</artifactId><version>0.0.1-SNAPSHOT</version><name>starter</name><description>SpringBoot自定義starter</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies><build><plugins><!--提供source--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><configuration><attach>true</attach></configuration><executions><execution><phase>compile</phase><goals><goal>jar</goal></goals></execution></executions></plugin></plugins></build></project>創(chuàng)建 兩個(gè)Module: artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer
同樣的方式創(chuàng)建
artisan-spring-boot-starter (空的jar文件,僅僅提供輔助性依賴管理)
pom
<?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"><parent><artifactId>starter</artifactId><groupId>com.artisan.customstarter</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><description>啟動(dòng)器(starter)是一個(gè)空的jar文件,僅僅提供輔助性依賴管理,這些依賴需要自動(dòng)裝配或其他類庫。</description><artifactId>artisan-spring-boot-starter</artifactId><dependencies><!--引入autoconfigure--><dependency><groupId>com.artisan.customstarter</groupId><artifactId>artisan-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!--如果當(dāng)前starter 還需要其他的類庫就在這里引用--></dependencies></project>如果使用spring Initializr創(chuàng)建的需要?jiǎng)h除 啟動(dòng)類、resources下的文件,test文件。
artisan-spring-boot-starter-autoconfigurer
pom
<?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"><parent><artifactId>starter</artifactId><groupId>com.artisan.customstarter</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>artisan-spring-boot-starter-autoconfigurer</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--‐導(dǎo)入配置文件處理器,配置文件進(jìn)行綁定就會(huì)有提示--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies></project>關(guān)于自動(dòng)提示的jar ,引入spring-boot-configuration-processor之后, 編譯之后就會(huì)在META-INF文件夾下面生成一個(gè)spring-configuration-metadata.json的文件。
內(nèi)容如下
【工程結(jié)構(gòu)】
CustomProperties
package com.artisan;import org.springframework.boot.context.properties.ConfigurationProperties;/*** @author 小工匠* @version 1.0* @description: 屬性配置文件* @date 2021/5/22 18:56* @mark: show me the code , change the world*/@ConfigurationProperties("artisan.custom") public class CustomProperties {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} }IndexController
package com.artisan;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author 小工匠* @version 1.0* @description: TODO* @date 2021/5/22 18:58* @mark: show me the code , change the world*/@RestController public class IndexController {private CustomProperties customProperties;public IndexController(CustomProperties customProperties) {this.customProperties = customProperties;}@RequestMapping("/")public String index() {return customProperties.getName();}}CustomAutoConfiguration 自動(dòng)配置類
package com.artisan;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** @author 小工匠* @version 1.0* @description: 模擬給web應(yīng)用自動(dòng)添加一個(gè)首頁* @date 2021/5/22 18:55* @mark: show me the code , change the world*/@Configuration @ConditionalOnProperty(value = "artisan.custom.name") @EnableConfigurationProperties(CustomProperties.class) public class CustomAutoConfiguration {@AutowiredCustomProperties cus;@Beanpublic IndexController indexController() {return new IndexController(cus);}}spring.factories
在 resources 下創(chuàng)建文件夾 META-INF 并在 META-INF 下創(chuàng)建文件 spring.factories ,內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.artisan.CustomAutoConfiguration打包發(fā)布
到這兒,我們的配置自定義的starter就寫完了 ,我們artisan-spring-boot-starter 和 artisan-spring-boot-starter-autoconfigurer 安裝成本地jar包。
引用自定義starter測試
新建個(gè)spring boot 項(xiàng)目 ,引用剛才的starter
訪問 http://localhost:8080/
咦 ,別忘了你自定義的屬性
重啟后,重新訪問
因?yàn)槲覀冮_啟了debug=true .找找我們自動(dòng)裝配的類看看
總結(jié)
以上是生活随笔為你收集整理的Spring Boot - 手把手教小师妹自定义Spring Boot Starter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot - 自动配置实例
- 下一篇: SpringBoot - 探究Sprin