最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot中自定義Starter的完整教程

 更新時間:2025年11月14日 10:22:54   作者:杰西筆記  
在 Spring Boot 中,自定義 Starter 是一種非常強大的方式,可以將常用的功能模塊封裝成可復用的組件,下面我們就來看看具體的實現(xiàn)方法吧

前言

在 Spring Boot 中,自定義 Starter 是一種非常強大的方式,可以將常用的功能模塊封裝成可復用的組件。本文將詳細介紹如何創(chuàng)建一個自定義 Starter,并提供完整的使用流程和代碼示例。

一、自定義 Starter 原理

Spring Boot 的自動配置機制依賴于 META-INF/spring.factories 文件中的 EnableAutoConfiguration 配置項。當項目啟動時,Spring Boot 會掃描所有 META-INF/spring.factories 文件,加載其中指定的自動配置類。

啟動流程圖解:

starter → autoconfigure → spring-boot-starter
         ↑
         └─ META-INF/spring.factories

  • starter:Maven/Gradle 依賴包(啟動器)
  • autoconfigure:包含自動配置邏輯的包
  • spring.factories:配置自動配置類的入口文件

二、項目結(jié)構(gòu)設(shè)計

我們以一個簡單的“Hello World”功能為例,創(chuàng)建一個名為 atguigu-hello-spring-boot-starter 的自定義 Starter。

模塊劃分

├── atguigu-hello-spring-boot-starter (Starter)
│   ├── pom.xml
│   └── src/main/java/com/atguigu/hello/HelloService.java

├── atguigu-hello-spring-boot-starter-autoconfigure (Autoconfigure)
│   ├── pom.xml
│   ├── src/main/java/com/atguigu/hello/config/HelloAutoConfiguration.java
│   ├── src/main/java/com/atguigu/hello/config/HelloProperties.java
│   └── src/main/resources/META-INF/spring.factories
 

三、代碼實現(xiàn)

1. 創(chuàng)建 Autoconfigure 模塊

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.atguigu</groupId>
    <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
</project>

HelloProperties.java(配置屬性類)

package com.atguigu.hello.config;

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

@Component
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {

    private String message = "Hello, World!";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

HelloAutoConfiguration.java(自動配置類)

package com.atguigu.hello.config;

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

@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public HelloService helloService(HelloProperties properties) {
        return new HelloService(properties.getMessage());
    }
}

HelloService.java(業(yè)務(wù)邏輯類)

package com.atguigu.hello;

public class HelloService {

    private final String message;

    public HelloService(String message) {
        this.message = message;
    }

    public String sayHello() {
        return message;
    }

    public void printHello() {
        System.out.println(sayHello());
    }
}

META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.atguigu.hello.config.HelloAutoConfiguration

注意:spring.factories 文件必須放在 src/main/resources/META-INF/ 目錄下。

2. 創(chuàng)建 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu</groupId>
    <artifactId>atguigu-hello-spring-boot-starter</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!-- 引入 autoconfigure 包 -->
        <dependency>
            <groupId>com.atguigu</groupId>
            <artifactId>atguigu-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

說明:這個 starter 只是一個“門面”,真正實現(xiàn)自動配置的是 autoconfigure 模塊。

四、使用自定義 Starter

1. 在目標項目中引入 Starter

pom.xml

<dependency>
    <groupId>com.atguigu</groupId>
    <artifactId>atguigu-hello-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

2. 配置文件application.yml

hello:
  message: "Hello, Spring Boot!"

3. 編寫測試代碼

@SpringBootApplication
public class DemoApplication {

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

    @Autowired
    private HelloService helloService;

    @PostConstruct
    public void init() {
        helloService.printHello(); // 輸出: Hello, Spring Boot!
    }
}

五、完整流程總結(jié)

步驟描述
1創(chuàng)建 autoconfigure 模塊,包含自動配置邏輯
2編寫 @Configuration 類,使用 @EnableConfigurationProperties 綁定配置
3在 META-INF/spring.factories 中注冊自動配置類
4創(chuàng)建 starter 模塊,僅依賴 autoconfigure
5在主項目中引入 starter,無需額外配置
6通過 application.yml 設(shè)置參數(shù),自動生效

六、關(guān)鍵注解說明

注解作用
@Configuration標記為配置類
@EnableConfigurationProperties啟用配置屬性綁定
@ConditionalOnMissingBean當容器中沒有該 Bean 時才創(chuàng)建
@Component將類注冊為 Spring Bean
@ConfigurationProperties綁定配置文件前綴屬性

七、擴展建議

  • 支持多環(huán)境配置(如 dev, prod
  • 添加日志記錄或監(jiān)控功能
  • 使用 @ConditionalOnClass@ConditionalOnProperty 實現(xiàn)更復雜的條件判斷
  • 提供接口供外部擴展

八、注意事項

  • spring.factories 文件格式必須正確,不能有空格或換行錯誤。
  • 確保 autoconfigure 模塊的 spring-boot-autoconfigure 依賴已添加。
  • 啟動器模塊不要直接寫業(yè)務(wù)邏輯,只做依賴管理。
  • 版本號保持一致,避免沖突。

九、運行效果

啟動項目后,控制臺輸出

Hello, Spring Boot!

說明自定義 Starter 成功加載并執(zhí)行了自動配置邏輯。

總結(jié):通過這種方式,你可以輕松地將任何功能封裝為可復用的 Spring Boot Starter,提升開發(fā)效率與代碼復用性。

到此這篇關(guān)于SpringBoot中自定義Starter的完整教程的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JVM內(nèi)存參數(shù)配置詳解

    JVM內(nèi)存參數(shù)配置詳解

    本文主要介紹了JVM內(nèi)存參數(shù)配置詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot實現(xiàn)模塊日志入庫的項目實踐

    SpringBoot實現(xiàn)模塊日志入庫的項目實踐

    本文主要介紹了SpringBoot實現(xiàn)模塊日志入庫的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Java?-jar參數(shù)詳解之掌握Java可執(zhí)行JAR文件的運行技巧

    Java?-jar參數(shù)詳解之掌握Java可執(zhí)行JAR文件的運行技巧

    做項目的時候我們肯定接觸過很多jar包,下面這篇文章主要給大家介紹了關(guān)于Java?-jar參數(shù)詳解之掌握Java可執(zhí)行JAR文件的運行技巧,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • java執(zhí)行shell并獲取shell輸出日志方式

    java執(zhí)行shell并獲取shell輸出日志方式

    這篇文章主要介紹了java執(zhí)行shell并獲取shell輸出日志方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • java如何獲取兩個日期的時間差

    java如何獲取兩個日期的時間差

    這篇文章主要為大家詳細介紹了java獲取兩個日期時間差的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • IDEA中database使用教程

    IDEA中database使用教程

    idea集成了一個數(shù)據(jù)庫管理工具,可以可視化管理很多種類的數(shù)據(jù)庫,本文主要介紹了IDEA中database使用教程,具有一定的參考價值,感興趣的可以了解一下
    2023-06-06
  • Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果

    Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果

    這篇文章主要為大家詳細介紹了Java OpenCV實現(xiàn)圖像鏡像翻轉(zhuǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • java制作簡單驗證碼功能

    java制作簡單驗證碼功能

    這篇文章主要為大家詳細介紹了java制作簡單驗證碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項

    Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項

    這篇文章主要給大家介紹了關(guān)于Spring Cloud Ubuntu環(huán)境部署的步驟與注意事項,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Cloud具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-12-12
  • 詳解Spring AOP的實現(xiàn)方式

    詳解Spring AOP的實現(xiàn)方式

    AOP是一種思想,是對某一類事情的集中處理,切面就是指某一類特定的問題,所以AOP可以理解為面向特定方法編程,這篇文章主要介紹了Spring AOP的實現(xiàn)方式,需要的朋友可以參考下
    2024-02-02

最新評論

彩票| 上栗县| 随州市| 修武县| 呈贡县| 尼玛县| 澎湖县| 上虞市| 通榆县| 临沧市| 汕尾市| 五华县| 庐江县| 茶陵县| 广汉市| 尤溪县| 历史| 灯塔市| 宝山区| 梁山县| 泗水县| 定襄县| 新津县| 繁峙县| 喜德县| 调兵山市| 贵溪市| 湘潭市| 乌拉特后旗| 绥阳县| 肃宁县| 崇仁县| 商丘市| 桦甸市| 五莲县| 威信县| 西城区| 金堂县| 西乌| 宁城县| 宜黄县|