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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

【SpringBoot】编写一个自己的Starter

發(fā)布時(shí)間:2025/3/21 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SpringBoot】编写一个自己的Starter 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、什么是Starter?

在開(kāi)發(fā)過(guò)程中我們就經(jīng)常使用到各種starter,比如mybatis-spring-boot-starter,只需要進(jìn)行簡(jiǎn)單的配置即可使用,就像一個(gè)插件非常方便。這也是SpringBoot非常重要的一個(gè)特性——自動(dòng)化配置。

二、實(shí)現(xiàn)

2.1創(chuàng)建一個(gè)maven項(xiàng)目并配置pom.xml

命名規(guī)范: Spring官方的Starter命名格式一般是spring-boot-starter-{name},比如spring-boot-starter-web 。而非官方的,官方建議artifactId命名應(yīng)該遵循 {name}-spring-boot-starter的格式,如example-spring-boot-starter。

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">?
  • <modelVersion>4.0.0</modelVersion>?
  • ?
  • <groupId>cn.sp</groupId>?
  • <artifactId>example-spring-boot-starter</artifactId>?
  • <version>1.0-SNAPSHOT</version>?
  • ?
  • <properties>?
  • <spring-boot.version>2.1.5.RELEASE</spring-boot.version>?
  • </properties>?
  • ?
  • <dependencies>?
  • <dependency>?
  • <groupId>org.springframework.boot</groupId>?
  • <artifactId>spring-boot-configuration-processor</artifactId>?
  • <optional>true</optional>?
  • </dependency>?
  • ?
  • <dependency>?
  • <groupId>org.springframework.boot</groupId>?
  • <artifactId>spring-boot-autoconfigure</artifactId>?
  • </dependency>?
  • </dependencies>?
  • ?
  • ?
  • <dependencyManagement>?
  • <dependencies>?
  • <dependency>?
  • <!-- Import dependency management from Spring Boot -->?
  • <groupId>org.springframework.boot</groupId>?
  • <artifactId>spring-boot-dependencies</artifactId>?
  • <version>${spring-boot.version}</version>?
  • <type>pom</type>?
  • <scope>import</scope>?
  • </dependency>?
  • </dependencies>?
  • </dependencyManagement>?
  • ?
  • </project>?
  • spring-boot-configuration-processor 的作用是編譯時(shí)生成 spring-configuration-metadata.json ,此文件主要給IDE使用,ctlr+鼠標(biāo)左鍵點(diǎn)擊配置文件(如application.properties)上相關(guān)配置屬性,即可跳轉(zhuǎn)到配置此屬性的類中。

    我們要實(shí)現(xiàn)的一個(gè)小功能是讀取配置文件上cn.sp.config的字符串,然后按照給定的分隔符進(jìn)行分割。

    2.2編寫(xiě)配置文件讀取類

    @ConfigurationProperties(prefix = "cn.sp") public class StarterServiceProperties {private String config;public String getConfig() {return config;}public void setConfig(String config) {this.config = config;} }

    2.3編寫(xiě)Service

    public class StarterService {private String config;public StarterService(String config){this.config = config;}public String[] split(String separatorChar){return this.config.split(separatorChar);} }

    2.4編寫(xiě)自動(dòng)配置類(重點(diǎn))

  • package cn.sp.autoconfigure;?
  • ?
  • import org.springframework.beans.factory.annotation.Autowired;?
  • import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;?
  • import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;?
  • 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;?
  • ?
  • /**?
  • * Created by 2YSP on 2019/5/22.?
  • */?
  • @Configuration?
  • @ConditionalOnClass(StarterService.class)?
  • //@ConditionalOnProperty(prefix = "cn.sp",value = "enable",matchIfMissing = true)?
  • @EnableConfigurationProperties(StarterServiceProperties.class)?
  • public class StarterAutoConfigure {?
  • ?
  • @Autowired?
  • private StarterServiceProperties properties;?
  • ?
  • @Bean?
  • @ConditionalOnMissingBean?
  • @ConditionalOnProperty(prefix = "cn.sp",value = "enabled",havingValue = "true")?
  • StarterService starterService(){?
  • return new StarterService(properties.getConfig());?
  • }?
  • ?
  • }?
  • ?
  • 說(shuō)下這幾個(gè)注解的作用:

  • @ConditionalOnClass:當(dāng)classpath下發(fā)現(xiàn)該類的情況下進(jìn)行自動(dòng)配置。
  • @EnableConfigurationProperties:使使用 @ConfigurationProperties 注解的類生效。具體可以參考https://www.jianshu.com/p/7f54da1cb2eb
  • @ConditionalOnMissingBean:當(dāng)Spring上下文中不存在該Bean時(shí)生效。
  • @ConditionalOnProperty(prefix = "cn.sp",value = "enabled",havingValue = "true"),當(dāng)配置文件中cn.sp.enabled=true時(shí)有效。
  • 下面列舉SpringBoot中的所有@Conditional注解及作用

    @ConditionalOnBean:當(dāng)容器中有指定的Bean的條件下
    @ConditionalOnClass:當(dāng)類路徑下有指定的類的條件下
    @ConditionalOnExpression:基于SpEL表達(dá)式作為判斷條件
    @ConditionalOnJava:基于JVM版本作為判斷條件
    @ConditionalOnJndi:在JNDI存在的條件下查找指定的位置
    @ConditionalOnMissingBean:當(dāng)容器中沒(méi)有指定Bean的情況下
    @ConditionalOnMissingClass:當(dāng)類路徑下沒(méi)有指定的類的條件下
    @ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是Web項(xiàng)目的條件下
    @ConditionalOnProperty:指定的屬性是否有指定的值
    @ConditionalOnResource:類路徑下是否有指定的資源
    @ConditionalOnSingleCandidate:當(dāng)指定的Bean在容器中只有一個(gè),或者在有多個(gè)Bean的情況下,用來(lái)指定首選的Bean
    @ConditionalOnWebApplication:當(dāng)前項(xiàng)目是Web項(xiàng)目的條件下

    2.5創(chuàng)建spring.factories

    resources/META-INF/ 文件夾下創(chuàng)建spring.factories文件,內(nèi)容如下:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.sp.autoconfigure.StarterAutoConfigure

    右邊的就是自動(dòng)配置類的類路徑,注意單詞別打錯(cuò)了,我就是META-INF打成了MATA-INF害我折騰了半天。

    三、測(cè)試

  • 執(zhí)行mvn install命令打包到本地
  • 在另外一個(gè)項(xiàng)目添加依賴
  • <dependency><groupId>cn.sp</groupId><artifactId>example-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>

    然后可以看到j(luò)ar包的結(jié)構(gòu)圖如下:

    3. 在application.properties文件添加如下內(nèi)容

    cn.sp.enabled=true cn.sp.config=fdafdf,ss1,DSDS,DDD
  • 編寫(xiě)測(cè)試類并啟動(dòng)
  • @RunWith(SpringRunner.class) @SpringBootTest public class MySpringbootApplicationTests {@AutowiredStarterService starterService;@Testpublic void contextLoads() {String[] strings = starterService.split(",");for (int i = 0; i < strings.length; i++) {System.out.println(strings[i]);}}}
  • 運(yùn)行結(jié)果如下則表示成功。
  • 2019-05-23 10:41:49.219 [main] INFO cn.sp.MySpringbootApplicationTests - Started MySpringbootApplicationTests in 10.977 seconds (JVM running for 13.035)
    fdafdf
    ss1
    DSDS
    DDD

    2019-05-23 10:41:52.411 [Thread-4] INFO o.s.w.c.s.GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@51f49060: startup date [Thu May 23 10:41:38 CST 2019]; root of context hierarchy

    四、原理

    1.在應(yīng)用程序啟動(dòng)過(guò)程中,Spring Boot使用SpringFactoriesLoader類加載器查找org.springframework.boot.autoconfigure.EnableAutoConfiguration關(guān)鍵字對(duì)應(yīng)的Java配置文件。Spring Boot會(huì)遍歷在各個(gè)jar包中META-INF目錄下的spring.factories文件,構(gòu)建成一個(gè)配置文件鏈表。
    2.根據(jù)spring.factories配置加載AutoConfigure類
    3.根據(jù) @Conditional注解的條件,進(jìn)行自動(dòng)配置并將Bean注入Spring Context中。
    注意: Spring Boot的starter在編譯時(shí)不需要依賴Spring Boot的庫(kù)。
    代碼地址:https://github.com/2YSP/example-spring-boot-starter
    參考:
    https://juejin.im/entry/58d37630570c350058c2c15c
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html

    轉(zhuǎn)載于:https://www.cnblogs.com/2YSP/p/10911166.html

    總結(jié)

    以上是生活随笔為你收集整理的【SpringBoot】编写一个自己的Starter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。