# Spring Boot 實(shí)戰(zhàn)系列 - 開發(fā)你自己的Spring Boot Starter

Spring Boot Starter 簡(jiǎn)介

  • Spring Boot Starter 是一組方便使用的依賴關(guān)系描述符,可以在應(yīng)用程序中包含這些描述符旁仿。借助 Spring Boot Starter 開發(fā)人員可以獲得所需的所有 Spring 及相關(guān)技術(shù)的一站式服務(wù)妆够,而無需查看示例代碼或復(fù)制粘貼依賴的庫(kù)文件商玫。譬如狂打,如果需要 Spring JPA 訪問數(shù)據(jù)庫(kù)蝌借,則可以在工程中直接飲用 spring-boot-starter-data-jpa
  • 有關(guān) starter 命名規(guī)范昔瞧,所有官方發(fā)布的 starter 都遵循以下命名模式 spring-boot-starter-*,其中 * 指特定的應(yīng)用程序代號(hào)或名稱菩佑。任何第三方提供的 starter 都不能以 spring-boot 作為前綴自晰,應(yīng)該將應(yīng)用程序代號(hào)或名稱作為前綴,譬如 mybatis-spring-boot-starter

Spring Boot Starter 開發(fā)步驟

本次以 redisson 為例稍坯,開發(fā)自己的 starter 酬荞,命名為redisson-spring-boot-starter

第一步 新建 maven 工程 redisson-spring-boot-starter

相信各位小伙都非常熟練,不多說了瞧哟,省略

第二步 添加 pom 依賴混巧,完整 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.lushwe</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring.boot.version>2.0.4.RELEASE</spring.boot.version>
        <redisson.version>3.4.4</redisson.version>
    </properties>

    <dependencies>

        <!-- spring boot autoconfigure 依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

        <!-- redisson 依賴 -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>${redisson.version}</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

備注:

  • <optional>true</optional> 表示當(dāng)前依賴包是 可選 的,也就是說勤揩,我們引入 redisson-spring-boot-starter 默認(rèn)不會(huì)引入 redisson 依賴咧党,好處就是,我們可以先引入 redisson-spring-boot-starter 陨亡, 然后在需要用到 redisson 功能時(shí)再引入 redisson

第二步 新建 redisson 的配置屬性類傍衡,完整 RedissonProperties 類如下

package org.redisson.spring.boot.autoconfigure;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 說明:Redisson的配置屬性
 *
 * @author Jack Liu
 * @date 2019-06-13 15:31
 * @since 1.0
 */
@ConfigurationProperties(prefix = RedissonProperties.MYBATIS_PREFIX)
public class RedissonProperties {

    /**
     * 配置文件前綴
     */
    public static final String MYBATIS_PREFIX = "redisson";

    /**
     * 配置文件路徑
     */
    private String configLocation;


    public String getConfigLocation() {
        return configLocation;
    }

    public void setConfigLocation(String configLocation) {
        this.configLocation = configLocation;
    }

}

備注:

  • @ConfigurationProperties(prefix = RedissonProperties.MYBATIS_PREFIX) 表示前綴為 redisson 的屬性才會(huì)注入到 RedissonProperties

第三步 新建 redisson 的自動(dòng)配置類,完整 RedissonAutoConfiguration 類如下

package org.redisson.spring.boot.autoconfigure;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
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;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;

/**
 * 說明:Redisson自動(dòng)配置
 *
 * @author Jack Liu
 * @date 2019-06-13 15:31
 * @since 1.0
 */
@Configuration
@ConditionalOnClass({RedissonClient.class})
@ConditionalOnProperty(prefix = "redisson", name = "config-location")
@EnableConfigurationProperties({RedissonProperties.class})
public class RedissonAutoConfiguration {

    private final ResourceLoader resourceLoader;

    private final RedissonProperties properties;

    // 構(gòu)造方法注入
    public RedissonAutoConfiguration(RedissonProperties properties, ResourceLoader resourceLoader) {
        this.properties = properties;
        this.resourceLoader = resourceLoader;
    }

    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean
    public RedissonClient redissonClient() throws IOException {
        Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
        Config config = null;
        if (this.properties.getConfigLocation().endsWith(".yaml")) {
            config = Config.fromYAML(resource.getFile());
        } else if (this.properties.getConfigLocation().endsWith(".json")) {
            config = Config.fromJSON(resource.getFile());
        }
        return config != null ? Redisson.create(config) : Redisson.create();
    }

}

備注:

  • @ConditionalOnClass({RedissonClient.class}) 表示 classpath 下有 RedissonClient 類才會(huì)啟用 RedissonAutoConfiguration 负蠕。也就是說蛙埂,沒有引入 redisson 依賴包,RedissonAutoConfiguration 不會(huì)生效
  • @ConditionalOnProperty(prefix = "redisson", name = "config-location") 表示配置文件有 redisson.config-location 屬性遮糖, 才會(huì)啟用 RedissonAutoConfiguration 绣的。

第四步 創(chuàng)建配置,新建 redisson 的自動(dòng)配置類,完整 RedissonAutoConfiguration 類如下

  • resource 目錄下屡江,創(chuàng)建 META-INF 目錄
  • META-INF 目錄下創(chuàng)建 spring.factories 文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.redisson.spring.boot.autoconfigure.RedissonAutoConfiguration
  • META-INF 目錄下創(chuàng)建 spring.provides 文件
provides: redisson-spring-boot-starter

第五步 至此芭概,redisson starter 開發(fā)完成,打包結(jié)束

完整項(xiàng)目地址

Spring Boot Starter 使用

resisson-spring-boot-starter 已經(jīng)開發(fā)完成惩嘉,下面演示如何使用我們自己開發(fā)的 starter

  • 新建一個(gè)工程谈山,引入上面開發(fā)好的 resisson-spring-boot-starter ,完整 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">
    <parent>
        <artifactId>learn-spring-boot</artifactId>
        <groupId>com.lushwe.learn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-starter-test</artifactId>

    <properties>
        <spring.boot.version>2.0.4.RELEASE</spring.boot.version>
        <redisson.version>3.4.4</redisson.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>com.lushwe</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>${redisson.version}</version>
        </dependency>

    </dependencies>

</project>
  • 創(chuàng)建 application.properties 文件宏怔,配置 redisson.config-location 奏路,詳細(xì)如下
# Redisson 配置
redisson.config-location=classpath:redisson-redis.yaml
  • 創(chuàng)建 redisson-redis.yaml 文件,詳細(xì)如下
masterSlaveServersConfig:
  slaveAddresses:
    - "redis://10.0.10.23:15489"
  masterAddress: "redis://10.0.10.23:15389"
  password: codis*23
  idleConnectionTimeout: 10000
  pingTimeout: 1000
  connectTimeout: 10000
  timeout: 3000
  retryAttempts: 3
  retryInterval: 1500
  reconnectionTimeout: 3000
  failedAttempts: 3
  subscriptionsPerConnection: 5
  clientName: null
  loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
  slaveSubscriptionConnectionMinimumIdleSize: 1
  slaveSubscriptionConnectionPoolSize: 50
  slaveConnectionMinimumIdleSize: 10
  slaveConnectionPoolSize: 64
  masterConnectionMinimumIdleSize: 10
  masterConnectionPoolSize: 64
  readMode: "SLAVE"
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
useLinuxNativeEpoll: false
  • 創(chuàng)建一個(gè)啟動(dòng)類 Application 臊诊,詳細(xì)如下
package com.lushwe.spring.boot.start.test;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 說明:應(yīng)用啟動(dòng)類
 *
 * @author Jack Liu
 * @date 2019-06-12 13:28
 * @since 1.0
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

        System.out.println(context.getBean("redissonClient"));

        RedissonClient redissonClient = context.getBean(RedissonClient.class);

        System.out.println(redissonClient);

        RBucket<String> bucket1 = redissonClient.getBucket("redisson-v");
        bucket1.set("lushwe1");
        System.out.println(bucket1.get());

        RBucket<String> bucket2 = redissonClient.getBucket("redisson-v");
        bucket2.set("lushwe2");
        System.out.println(bucket2.get());

        RBucket<String> bucket3 = redissonClient.getBucket("redisson-v");
        System.out.println(bucket3.get());


    }
}

運(yùn)行 Application 鸽粉,日志打印如下

日志顯示,我們能正常使用 RedissonClient


本文主要介紹了如何開發(fā)你自己的 starter 抓艳,以及如何使用 starter 触机,比較簡(jiǎn)單,如有什么問題歡迎指正和提問玷或。

本文完

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末儡首,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子偏友,更是在濱河造成了極大的恐慌蔬胯,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件位他,死亡現(xiàn)場(chǎng)離奇詭異氛濒,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鹅髓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門舞竿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人窿冯,你說我怎么就攤上這事骗奖。” “怎么了醒串?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵执桌,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我厦凤,道長(zhǎng)鼻吮,這世上最難降的妖魔是什么育苟? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任较鼓,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘博烂。我一直安慰自己香椎,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布禽篱。 她就那樣靜靜地躺著畜伐,像睡著了一般。 火紅的嫁衣襯著肌膚如雪躺率。 梳的紋絲不亂的頭發(fā)上玛界,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音悼吱,去河邊找鬼慎框。 笑死,一個(gè)胖子當(dāng)著我的面吹牛后添,可吹牛的內(nèi)容都是我干的笨枯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼遇西,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼馅精!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起粱檀,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤洲敢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后茄蚯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沦疾,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年第队,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了哮塞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凳谦,死狀恐怖忆畅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情尸执,我是刑警寧澤家凯,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站如失,受9級(jí)特大地震影響绊诲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜褪贵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一掂之、第九天 我趴在偏房一處隱蔽的房頂上張望抗俄。 院中可真熱鬧,春花似錦世舰、人聲如沸动雹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)胰蝠。三九已至,卻和暖如春震蒋,著一層夾襖步出監(jiān)牢的瞬間茸塞,已是汗流浹背芝加。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工建瘫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人啊掏。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓梗搅,卻偏偏與公主長(zhǎng)得像禾唁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子无切,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355