快速開發(fā)一個自定義Spring Boot Starter集绰,并使用它规辱。

眾所周知(不知道?點此)栽燕,Spring Boot由眾多Starter組成罕袋,隨著版本的推移Starter家族成員也與日俱增。在傳統(tǒng)Maven項目中通常將一些層碍岔、組件拆分為模塊來管理浴讯,以便相互依賴復用,在Spring Boot項目中我們則可以創(chuàng)建自定義Spring Boot Starter來達成該目的蔼啦。

好榆纽,開始,先創(chuàng)建一個Maven項目并引入依賴询吴,pom.xml如下掠河,供參考~

<?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>com.example</groupId>
    <artifactId>example-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <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>1.5.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

這里說下artifactId的命名問題,Spring 官方 Starter通常命名為spring-boot-starter-{name}spring-boot-starter-web猛计, Spring官方建議非官方Starter命名應遵循{name}-spring-boot-starter的格式唠摹。

這里講一下我們的Starter要實現(xiàn)的功能,很簡單奉瘤,提供一個Service勾拉,包含一個能夠將字符串加上前后綴的方法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)內的參數(shù)獲得

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

重點藕赞,編寫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());
    }

}

解釋下用到的幾個和Starter相關的注解:

  • @ConditionalOnClass,當classpath下發(fā)現(xiàn)該類的情況下進行自動配置卖局。
  • @ConditionalOnMissingBean斧蜕,當Spring Context中不存在該Bean時。
  • @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")砚偶,當配置文件中example.service.enabled=true時批销。

更多相關注解,建議閱讀官方文檔該部分染坯。

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

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

OK单鹿,完事掀宋,運行 mvn:install打包安裝,一個Spring Boot Starter便開發(fā)完成了。如果你需要該Starter的源代碼劲妙,點這里湃鹊。


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

引入example-spring-boot-starter依賴

 <dependency>
    <groupId>com.example</groupId>
    <artifactId>example-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
 </dependency>

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

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

創(chuàng)建一個簡單的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);
    }

}

啟動Application涛舍,訪問/input接口試試看~


總結下Starter的工作原理

  1. Spring Boot在啟動時掃描項目所依賴的JAR包,尋找包含spring.factories文件的JAR包
  2. 根據(jù)spring.factories配置加載AutoConfigure
  3. 根據(jù) @Conditional注解的條件唆途,進行自動配置并將Bean注入Spring Context
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末富雅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子肛搬,更是在濱河造成了極大的恐慌没佑,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件温赔,死亡現(xiàn)場離奇詭異蛤奢,居然都是意外死亡,警方通過查閱死者的電腦和手機陶贼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門啤贩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拜秧,你說我怎么就攤上這事痹屹。” “怎么了枉氮?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵志衍,是天一觀的道長。 經(jīng)常有香客問我聊替,道長楼肪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任惹悄,我火速辦了婚禮春叫,結果婚禮上,老公的妹妹穿的比我還像新娘泣港。我一直安慰自己象缀,他們只是感情好,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布爷速。 她就那樣靜靜地躺著,像睡著了一般霞怀。 火紅的嫁衣襯著肌膚如雪惫东。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音廉沮,去河邊找鬼颓遏。 笑死,一個胖子當著我的面吹牛滞时,可吹牛的內容都是我干的叁幢。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼坪稽,長吁一口氣:“原來是場噩夢啊……” “哼曼玩!你這毒婦竟也來了?” 一聲冷哼從身側響起窒百,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤黍判,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后篙梢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顷帖,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年渤滞,在試婚紗的時候發(fā)現(xiàn)自己被綠了贬墩。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡妄呕,死狀恐怖陶舞,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情趴腋,我是刑警寧澤吊说,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站优炬,受9級特大地震影響颁井,放射性物質發(fā)生泄漏。R本人自食惡果不足惜蠢护,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一雅宾、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧葵硕,春花似錦眉抬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至介评,卻和暖如春库北,著一層夾襖步出監(jiān)牢的瞬間爬舰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工寒瓦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留情屹,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓杂腰,卻偏偏與公主長得像垃你,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子喂很,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容