自定義SpringbootStarter

1.創(chuàng)建一個maven的項目鳖目,里面有兩個模塊

  • hehe-spring-boot-starter 對外暴露的模塊癣朗,方便外部引用
  • hehe-spring-boot-autoconfigure 自動裝載的類涧黄,starter的功能定義在這里面
  • springboot-hehe-starter 父項目
maven項目.png

2.父項目的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>
    <!-- 編寫那個版本的spring-boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>springboot-hehe-starter</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>hehe-spring-boot-starter</module>
        <module>hehe-spring-boot-autoconfigure</module>
    </modules>
    
  <!-- 需要使用的依賴讼昆,autoconfigure會使用到 -->
    <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>

3.編寫hehe-spring-boot-autoconfigure啟動項目
3.1.創(chuàng)建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>springboot-hehe-starter</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hehe-spring-boot-autoconfigure</artifactId>

    <dependencies>
        <!--   需要用到web編寫controller     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--‐導(dǎo)入配置文件處理器会喝,配置文件進(jìn)行綁定就會有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--   condition類     -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>

3.2 編寫properties類澡罚,支持用戶配置properties屬性,里面也可以設(shè)置一些默認(rèn)屬性

package com.starter.hehe;

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

@ConfigurationProperties("hehe")
public class IndexProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

3.3 編寫bean敬尺,這里寫了一個controller
···
package com.starter.hehe;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

IndexProperties indexProperties;

public IndexController(IndexProperties indexProperties) {
    this.indexProperties=indexProperties;
}

@RequestMapping("/index")
public String index(){
    return indexProperties.getName()+"歡迎您";
}

}
···
3.4 編寫自動配置類

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration // 配置類
@ConditionalOnProperty(value = "hehe.name") //application.properties有這個配置才會使用這個自動配置類
@EnableConfigurationProperties(IndexProperties.class) // 讓IndexProperties類注入配置屬性并被spring管理枚尼,變成一個bean
@ConditionalOnClass(StrUtil.class) // 當(dāng)項目中有這個類的時候會加載這個自動配置模塊
public class IndexAutoConfiguration {

    @Autowired
    IndexProperties indexProperties;

    @Bean
    public IndexController indexController(){
        return new IndexController(indexProperties);
    }

}

3.5 編寫spring.factories
在resource下面建立META-INF包,創(chuàng)建spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.starter.hehe.IndexAutoConfitguration

4.編寫hehe-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">
    <parent>
        <artifactId>springboot-hehe-starter</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

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

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>hehe-spring-boot-autoconfigure</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

5.執(zhí)行maven的install砂吞,打包到本地倉庫
6.其他springboot項目引入剛寫的starter項目,并引入hutool工具類署恍,否則不會自動裝載進(jìn)來

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

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.0</version>
        </dependency>

7.properties文件添加配置hehe.name=[hehe]
8.啟動項目,訪問http://localhost:8082/index

1637675149(1).png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蜻直,一起剝皮案震驚了整個濱河市盯质,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌概而,老刑警劉巖呼巷,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赎瑰,居然都是意外死亡王悍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門餐曼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來压储,“玉大人鲜漩,你說我怎么就攤上這事〖铮” “怎么了孕似?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長刮刑。 經(jīng)常有香客問我喉祭,道長,這世上最難降的妖魔是什么为朋? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任臂拓,我火速辦了婚禮厚脉,結(jié)果婚禮上习寸,老公的妹妹穿的比我還像新娘。我一直安慰自己傻工,他們只是感情好霞溪,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著中捆,像睡著了一般鸯匹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上泄伪,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天殴蓬,我揣著相機(jī)與錄音,去河邊找鬼蟋滴。 笑死染厅,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的津函。 我是一名探鬼主播肖粮,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼尔苦!你這毒婦竟也來了涩馆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤允坚,失蹤者是張志新(化名)和其女友劉穎魂那,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稠项,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡冰寻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了皿渗。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片斩芭。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡轻腺,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出划乖,到底是詐尸還是另有隱情贬养,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布琴庵,位于F島的核電站误算,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏迷殿。R本人自食惡果不足惜儿礼,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望庆寺。 院中可真熱鬧蚊夫,春花似錦、人聲如沸懦尝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽陵霉。三九已至琅轧,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間踊挠,已是汗流浹背乍桂。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留效床,地道東北人睹酌。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像扁凛,于是被迫代替她去往敵國和親忍疾。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容