自定義SpringbootStarter

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

  • hehe-spring-boot-starter 對(duì)外暴露的模塊,方便外部引用
  • hehe-spring-boot-autoconfigure 自動(dòng)裝載的類,starter的功能定義在這里面
  • springboot-hehe-starter 父項(xiàng)目
maven項(xiàng)目.png

2.父項(xià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">
    <modelVersion>4.0.0</modelVersion>
    <!-- 編寫那個(gè)版本的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會(huì)使用到 -->
    <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啟動(dòng)項(xiàng)目
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)行綁定就會(huì)有提示-->
        <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,這里寫了一個(gè)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 編寫自動(dòng)配置類

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有這個(gè)配置才會(huì)使用這個(gè)自動(dòng)配置類
@EnableConfigurationProperties(IndexProperties.class) // 讓IndexProperties類注入配置屬性并被spring管理,變成一個(gè)bean
@ConditionalOnClass(StrUtil.class) // 當(dāng)項(xiàng)目中有這個(gè)類的時(shí)候會(huì)加載這個(gè)自動(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,打包到本地倉(cāng)庫
6.其他springboot項(xiàng)目引入剛寫的starter項(xiàng)目,并引入hutool工具類,否則不會(huì)自動(dòng)裝載進(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.啟動(dòng)項(xiàng)目,訪問http://localhost:8082/index

1637675149(1).png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容