<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 Starter,實(shí)現(xiàn)組件化復(fù)用!

          共 3217字,需瀏覽 7分鐘

           ·

          2020-12-07 11:20

          點(diǎn)擊上方?泥瓦匠?輕松關(guān)注!

          及時(shí)獲取有趣有料的技術(shù)文章

          fd16abee88f9b936af0724734eb19f73.webp

          來源:jianshu.com/p/45538b44e04e

          眾所周知,Spring Boot由眾多Starter組成,隨著版本的推移Starter家族成員也與日俱增。在傳統(tǒng)Maven項(xiàng)目中通常將一些層、組件拆分為模塊來管理,以便相互依賴復(fù)用,在Spring Boot項(xiàng)目中我們則可以創(chuàng)建自定義Spring Boot Starter來達(dá)成該目的。

          好,開始,先創(chuàng)建一個(gè)Maven項(xiàng)目并引入依賴,pom.xml如下,供參考~


          <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.0modelVersion>
          ????<groupId>com.examplegroupId>
          ????<artifactId>example-spring-boot-starterartifactId>
          ????<version>1.0-SNAPSHOTversion>
          ????<dependencies>
          ????????<dependency>
          ????????????<groupId>org.springframework.bootgroupId>
          ????????????<artifactId>spring-boot-autoconfigureartifactId>
          ????????dependency>
          ????dependencies>
          ????<dependencyManagement>
          ????????<dependencies>
          ????????????<dependency>
          ????????????????
          ????????????????<groupId>org.springframework.bootgroupId>
          ????????????????<artifactId>spring-boot-dependenciesartifactId>
          ????????????????<version>1.5.2.RELEASEversion>
          ????????????????<type>pomtype>
          ????????????????<scope>importscope>
          ????????????dependency>
          ????????dependencies>
          ????dependencyManagement>
          project>

          這里說下artifactId的命名問題,Spring 官方 Starter通常命名為spring-boot-starter-{name}spring-boot-starter-web, Spring官方建議非官方Starter命名應(yīng)遵循{name}-spring-boot-starter的格式。

          這里講一下我們的Starter要實(shí)現(xiàn)的功能,很簡(jiǎn)單,提供一個(gè)Service,包含一個(gè)能夠?qū)⒆址由锨昂缶Y的方法String wrap(String word)

          public?class?ExampleService?{

          ????private?String?prefix;
          ????private?String?suffix;

          ????public?ExampleService(String?prefix,?String?suffix)?{
          ????????this.prefix?=?prefix;
          ????????this.suffix?=?suffix;
          ????}
          ????public?String?wrap(String?word)?{
          ????????return?prefix?+?word?+?suffix;
          ????}
          }

          前綴、后綴通過讀取application.properties(yml)內(nèi)的參數(shù)獲得

          @ConfigurationProperties("example.service")
          public?class?ExampleServiceProperties?{
          ????private?String?prefix;
          ????private?String?suffix;
          ????//省略?getter?setter

          重點(diǎn),編寫AutoConfigure

          @Configuration
          @ConditionalOnClass(ExampleService.class)
          @EnableConfigurationProperties(ExampleServiceProperties.class)
          public?class?ExampleAutoConfigure?{

          ????@Autowired
          ????private?ExampleServiceProperties?properties;

          ????@Bean
          ????@ConditionalOnMissingBean
          ????@ConditionalOnProperty(prefix?=?"example.service",value?=?"enabled",havingValue?=?"true")
          ????ExampleService?exampleService?(){
          ????????return??new?ExampleService(properties.getPrefix(),properties.getSuffix());
          ????}

          }

          解釋下用到的幾個(gè)和Starter相關(guān)的注解:

          • @ConditionalOnClass,當(dāng)classpath下發(fā)現(xiàn)該類的情況下進(jìn)行自動(dòng)配置。
          • @ConditionalOnMissingBean,當(dāng)Spring Context中不存在該Bean時(shí)。
          • @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true"),當(dāng)配置文件中example.service.enabled=true時(shí)。

          更多相關(guān)注解,建議閱讀官方文檔該部分。

          最后一步,在resources/META-INF/下創(chuàng)建spring.factories文件,內(nèi)容供參考下面~

          org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.ExampleAutoConfigure

          OK,完事,運(yùn)行 mvn:install打包安裝,一個(gè)Spring Boot Starter便開發(fā)完成了。如果你需要該Starter的源代碼,點(diǎn)這里。


          創(chuàng)建一個(gè)Spring Boot項(xiàng)目來 試試~

          引入example-spring-boot-starter依賴

          ?
          ????com.example
          ????example-spring-boot-starter
          ????1.0-SNAPSHOT
          ?

          創(chuàng)建application.properties,進(jìn)行配置

          example.service.enabled=true
          example.service.prefix=####
          example.service.suffix=@@@@

          創(chuàng)建一個(gè)簡(jiǎn)單的Spring Web Application,注入Starter提供的ExampleService看它能否正常工作~

          @SpringBootApplication
          @RestController
          public?class?Application?{

          ????public?static?void?main(String[]?args)?{
          ????????SpringApplication.run(Application.class,?args);
          ????}

          ????@Autowired
          ????private?ExampleService?exampleService;

          ????@GetMapping("/input")
          ????public?String?input(String?word){
          ????????return?exampleService.wrap(word);
          ????}

          }

          啟動(dòng)Application,訪問/input接口試試看~

          feafe4bc00d352f319a9bcdad2545100.webp

          總結(jié)下Starter的工作原理

          1. Spring Boot在啟動(dòng)時(shí)掃描項(xiàng)目所依賴的JAR包,尋找包含spring.factories文件的JAR包
          2. 根據(jù)spring.factories配置加載AutoConfigure
          3. 根據(jù) @Conditional注解的條件,進(jìn)行自動(dòng)配置并將Bean注入Spring Context



          8f2669a9325c4163d642aa79db2817d9.webp



          下方二維碼關(guān)注我

          ca8626fb383fe471898599b6e62b3b3d.webp

          互聯(lián)網(wǎng)草根,堅(jiān)持分享技術(shù)創(chuàng)業(yè)產(chǎn)品心得和總結(jié)~



          381618c18700efc7a6f83d732d246aeb.webp

          點(diǎn)擊“閱讀原文”,領(lǐng)取 2020 年最新免費(fèi)技術(shù)資料大全

          ↓↓↓
          瀏覽 93
          點(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>
                  日本私人色噜噜影院 | 亚洲欧美99 | 云盘流出真实操逼免费视频国产 | 午夜福利视频一区 | 国产一线二线www |