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

SpringBoot Starter自定義之創(chuàng)建可復(fù)用的自動(dòng)配置模塊方式

 更新時(shí)間:2025年04月15日 09:23:57   作者:程序媛學(xué)姐  
本文將詳細(xì)介紹如何設(shè)計(jì)和實(shí)現(xiàn)一個(gè)自定義的Spring Boot Starter,幫助讀者掌握這一強(qiáng)大技術(shù),提升代碼復(fù)用性和開(kāi)發(fā)效率,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

引言

Spring Boot Starter是Spring Boot生態(tài)系統(tǒng)的核心組成部分,它極大地簡(jiǎn)化了項(xiàng)目的依賴管理和配置過(guò)程。通過(guò)Starter機(jī)制,開(kāi)發(fā)者只需引入相應(yīng)的依賴,Spring Boot就能自動(dòng)完成復(fù)雜的配置工作。對(duì)于企業(yè)級(jí)應(yīng)用開(kāi)發(fā),我們經(jīng)常需要在多個(gè)項(xiàng)目中復(fù)用某些通用功能,這時(shí)創(chuàng)建自定義Starter就顯得尤為重要。

一、自定義Starter基礎(chǔ)知識(shí)

Spring Boot Starter本質(zhì)上是一組依賴項(xiàng)的集合,同時(shí)結(jié)合自動(dòng)配置類,為特定功能提供開(kāi)箱即用的體驗(yàn)。自定義Starter的核心目標(biāo)是將可復(fù)用的功能模塊化,讓其他項(xiàng)目能夠通過(guò)簡(jiǎn)單的依賴引入來(lái)使用這些功能。

一個(gè)標(biāo)準(zhǔn)的Spring Boot Starter通常由兩個(gè)主要組件構(gòu)成:自動(dòng)配置模塊和Starter模塊。自動(dòng)配置模塊包含功能的具體實(shí)現(xiàn)和配置類,而Starter模塊則作為一個(gè)空殼,僅依賴于自動(dòng)配置模塊和其他必要的依賴。這種分離設(shè)計(jì)使得功能實(shí)現(xiàn)與依賴管理解耦,提高了模塊的靈活性。

以下是自定義Starter的基本命名規(guī)范:

// 對(duì)于官方Starter,命名格式為:
spring-boot-starter-{功能名}

// 對(duì)于非官方Starter,命名格式為:
{項(xiàng)目名}-spring-boot-starter

命名規(guī)范的遵循有助于區(qū)分官方與第三方Starter,避免潛在的命名沖突。

二、創(chuàng)建自動(dòng)配置模塊

自動(dòng)配置模塊是Starter的核心,它包含了功能的具體實(shí)現(xiàn)和自動(dòng)配置類。

我們以創(chuàng)建一個(gè)簡(jiǎn)單的數(shù)據(jù)加密Starter為例,展示自動(dòng)配置模塊的創(chuàng)建過(guò)程。

2.1 項(xiàng)目結(jié)構(gòu)搭建

首先創(chuàng)建一個(gè)Maven項(xiàng)目,命名為encryption-spring-boot-autoconfigure,作為自動(dòng)配置模塊。

項(xiàng)目結(jié)構(gòu)如下:

encryption-spring-boot-autoconfigure/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── encryption/
│   │   │               ├── autoconfigure/
│   │   │               │   └── EncryptionAutoConfiguration.java
│   │   │               ├── properties/
│   │   │               │   └── EncryptionProperties.java
│   │   │               └── service/
│   │   │                   ├── EncryptionService.java
│   │   │                   └── impl/
│   │   │                       └── AESEncryptionServiceImpl.java
│   │   └── resources/
│   │       └── META-INF/
│   │           └── spring.factories
└── pom.xml

2.2 配置屬性類

創(chuàng)建配置屬性類,用于存儲(chǔ)和管理加密服務(wù)的相關(guān)配置:

package com.example.encryption.properties;

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

/**
 * 加密服務(wù)配置屬性類
 * 通過(guò)@ConfigurationProperties注解綁定配置文件中的屬性
 */
@ConfigurationProperties(prefix = "encryption")
public class EncryptionProperties {
    
    /**
     * 加密算法,默認(rèn)為AES
     */
    private String algorithm = "AES";
    
    /**
     * 加密密鑰
     */
    private String key = "defaultKey123456";
    
    /**
     * 是否啟用加密服務(wù)
     */
    private boolean enabled = true;
    
    // Getter和Setter方法
    public String getAlgorithm() {
        return algorithm;
    }
    
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }
    
    public String getKey() {
        return key;
    }
    
    public void setKey(String key) {
        this.key = key;
    }
    
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
}

2.3 服務(wù)接口及實(shí)現(xiàn)

定義加密服務(wù)接口及其實(shí)現(xiàn):

package com.example.encryption.service;

/**
 * 加密服務(wù)接口
 * 定義加密和解密的基本操作
 */
public interface EncryptionService {
    
    /**
     * 加密字符串
     * 
     * @param content 待加密內(nèi)容
     * @return 加密后的內(nèi)容
     */
    String encrypt(String content);
    
    /**
     * 解密字符串
     * 
     * @param encryptedContent 已加密內(nèi)容
     * @return 解密后的原文
     */
    String decrypt(String encryptedContent);
}

AES加密實(shí)現(xiàn)類:

package com.example.encryption.service.impl;

import com.example.encryption.properties.EncryptionProperties;
import com.example.encryption.service.EncryptionService;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * AES加密服務(wù)實(shí)現(xiàn)
 * 提供基于AES算法的加密和解密功能
 */
public class AESEncryptionServiceImpl implements EncryptionService {
    
    private final EncryptionProperties properties;
    
    public AESEncryptionServiceImpl(EncryptionProperties properties) {
        this.properties = properties;
    }
    
    @Override
    public String encrypt(String content) {
        try {
            // 創(chuàng)建密鑰規(guī)范
            SecretKeySpec keySpec = new SecretKeySpec(
                properties.getKey().getBytes(StandardCharsets.UTF_8),
                properties.getAlgorithm()
            );
            
            // 獲取Cipher實(shí)例
            Cipher cipher = Cipher.getInstance(properties.getAlgorithm());
            // 初始化為加密模式
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            
            // 執(zhí)行加密
            byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            // 返回Base64編碼后的加密結(jié)果
            return Base64.getEncoder().encodeToString(encrypted);
        } catch (Exception e) {
            throw new RuntimeException("加密失敗", e);
        }
    }
    
    @Override
    public String decrypt(String encryptedContent) {
        try {
            // 創(chuàng)建密鑰規(guī)范
            SecretKeySpec keySpec = new SecretKeySpec(
                properties.getKey().getBytes(StandardCharsets.UTF_8),
                properties.getAlgorithm()
            );
            
            // 獲取Cipher實(shí)例
            Cipher cipher = Cipher.getInstance(properties.getAlgorithm());
            // 初始化為解密模式
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            
            // 執(zhí)行解密
            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedContent));
            // 返回解密后的原文
            return new String(original, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException("解密失敗", e);
        }
    }
}

2.4 自動(dòng)配置類

創(chuàng)建自動(dòng)配置類,根據(jù)條件自動(dòng)裝配加密服務(wù):

package com.example.encryption.autoconfigure;

import com.example.encryption.properties.EncryptionProperties;
import com.example.encryption.service.EncryptionService;
import com.example.encryption.service.impl.AESEncryptionServiceImpl;
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;

/**
 * 加密服務(wù)自動(dòng)配置類
 * 負(fù)責(zé)根據(jù)條件自動(dòng)裝配加密服務(wù)
 */
@Configuration
@ConditionalOnClass(EncryptionService.class)
@EnableConfigurationProperties(EncryptionProperties.class)
@ConditionalOnProperty(prefix = "encryption", name = "enabled", havingValue = "true", matchIfMissing = true)
public class EncryptionAutoConfiguration {
    
    /**
     * 注冊(cè)加密服務(wù)Bean
     * 當(dāng)容器中不存在EncryptionService類型的Bean時(shí),創(chuàng)建默認(rèn)實(shí)現(xiàn)
     */
    @Bean
    @ConditionalOnMissingBean
    public EncryptionService encryptionService(EncryptionProperties properties) {
        return new AESEncryptionServiceImpl(properties);
    }
}

2.5 spring.factories文件

META-INF目錄下創(chuàng)建spring.factories文件,指定自動(dòng)配置類:

# 自動(dòng)配置類
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.encryption.autoconfigure.EncryptionAutoConfiguration

2.6 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>encryption-spring-boot-autoconfigure</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <spring-boot.version>2.7.0</spring-boot.version>
    </properties>

    <dependencies>
        <!-- Spring Boot AutoConfigure -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        
        <!-- 用于生成配置元數(shù)據(jù) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring-boot.version}</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

三、創(chuàng)建Starter模塊

Starter模塊是一個(gè)空殼模塊,它依賴于自動(dòng)配置模塊和其他必要的依賴,向使用者提供一站式的依賴引入體驗(yàn)。

3.1 項(xiàng)目結(jié)構(gòu)

創(chuàng)建一個(gè)Maven項(xiàng)目,命名為encryption-spring-boot-starter,作為Starter模塊:

encryption-spring-boot-starter/
└── pom.xml

3.2 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>encryption-spring-boot-starter</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 依賴自動(dòng)配置模塊 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>encryption-spring-boot-autoconfigure</artifactId>
            <version>1.0.0</version>
        </dependency>
        
        <!-- 依賴其他必要的庫(kù),根據(jù)功能需求添加 -->
    </dependencies>
</project>

四、使用自定義Starter

創(chuàng)建完成后,我們可以在其他項(xiàng)目中使用這個(gè)自定義的Starter。

4.1 添加依賴

在項(xiàng)目的pom.xml中添加依賴:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>encryption-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

4.2 配置屬性

application.propertiesapplication.yml中配置加密服務(wù)屬性:

# 開(kāi)啟加密服務(wù)
encryption.enabled=true
# 設(shè)置加密算法
encryption.algorithm=AES
# 設(shè)置加密密鑰
encryption.key=mySecretKey12345

4.3 使用示例

在業(yè)務(wù)代碼中注入并使用加密服務(wù):

package com.example.demo;

import com.example.encryption.service.EncryptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 加密服務(wù)演示控制器
 * 展示如何在業(yè)務(wù)代碼中使用自定義Starter提供的功能
 */
@RestController
public class EncryptionController {
    
    private final EncryptionService encryptionService;
    
    @Autowired
    public EncryptionController(EncryptionService encryptionService) {
        this.encryptionService = encryptionService;
    }
    
    @GetMapping("/encrypt")
    public String encrypt(@RequestParam String content) {
        return encryptionService.encrypt(content);
    }
    
    @GetMapping("/decrypt")
    public String decrypt(@RequestParam String content) {
        return encryptionService.decrypt(content);
    }
}

五、Starter高級(jí)特性

5.1 條件化配置

Spring Boot提供了豐富的條件注解,用于控制Bean的裝配條件,使自定義Starter更加靈活:

// 當(dāng)類路徑下存在指定類時(shí)生效
@ConditionalOnClass(name = "com.example.SomeClass")

// 當(dāng)Bean不存在時(shí)生效
@ConditionalOnMissingBean

// 當(dāng)配置屬性滿足條件時(shí)生效
@ConditionalOnProperty(prefix = "feature", name = "enabled", havingValue = "true")

// 當(dāng)環(huán)境為指定profile時(shí)生效
@ConditionalOnProfile("dev")

// 當(dāng)Web應(yīng)用為Servlet類型時(shí)生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)

5.2 自動(dòng)配置順序控制

在復(fù)雜場(chǎng)景下,可能需要控制多個(gè)自動(dòng)配置類的執(zhí)行順序,可以使用@AutoConfigureBefore@AutoConfigureAfter注解:

// 在指定自動(dòng)配置類之前執(zhí)行
@AutoConfigureBefore(OtherAutoConfiguration.class)

// 在指定自動(dòng)配置類之后執(zhí)行
@AutoConfigureAfter(OtherAutoConfiguration.class)

// 示例代碼
@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class EncryptionAutoConfiguration {
    // 配置代碼
}

5.3 配置元數(shù)據(jù)

為了提供更好的IDE支持,可以創(chuàng)建配置元數(shù)據(jù)文件:

{
  "groups": [
    {
      "name": "encryption",
      "type": "com.example.encryption.properties.EncryptionProperties",
      "sourceType": "com.example.encryption.properties.EncryptionProperties"
    }
  ],
  "properties": [
    {
      "name": "encryption.enabled",
      "type": "java.lang.Boolean",
      "description": "是否啟用加密服務(wù)",
      "sourceType": "com.example.encryption.properties.EncryptionProperties",
      "defaultValue": true
    },
    {
      "name": "encryption.algorithm",
      "type": "java.lang.String",
      "description": "加密算法",
      "sourceType": "com.example.encryption.properties.EncryptionProperties",
      "defaultValue": "AES"
    },
    {
      "name": "encryption.key",
      "type": "java.lang.String",
      "description": "加密密鑰",
      "sourceType": "com.example.encryption.properties.EncryptionProperties",
      "defaultValue": "defaultKey123456"
    }
  ],
  "hints": [
    {
      "name": "encryption.algorithm",
      "values": [
        {
          "value": "AES",
          "description": "AES加密算法"
        },
        {
          "value": "DES",
          "description": "DES加密算法"
        }
      ]
    }
  ]
}

配置元數(shù)據(jù)文件應(yīng)放在META-INF/spring-configuration-metadata.json路徑下,通常由spring-boot-configuration-processor自動(dòng)生成。

總結(jié)

Spring Boot Starter是一種強(qiáng)大的自動(dòng)配置機(jī)制,通過(guò)自定義Starter,我們可以將業(yè)務(wù)中的通用功能模塊化,實(shí)現(xiàn)代碼的高度復(fù)用。自定義Starter的核心在于合理設(shè)計(jì)自動(dòng)配置類和配置屬性類,讓用戶能夠通過(guò)簡(jiǎn)單的配置來(lái)定制功能行為。

在創(chuàng)建過(guò)程中,我們需要遵循Spring Boot的命名規(guī)范和最佳實(shí)踐,將自動(dòng)配置模塊與Starter模塊分離,提高靈活性。通過(guò)條件化配置和自動(dòng)配置順序控制,可以讓Starter在復(fù)雜場(chǎng)景中也能穩(wěn)定工作。

對(duì)于企業(yè)級(jí)應(yīng)用開(kāi)發(fā),自定義Starter是提升團(tuán)隊(duì)效率的關(guān)鍵工具,它不僅能簡(jiǎn)化項(xiàng)目配置,還能確保各個(gè)項(xiàng)目遵循統(tǒng)一的最佳實(shí)踐,降低維護(hù)成本。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java反射機(jī)制最詳解

    java反射機(jī)制最詳解

    這篇文章主要介紹了Java 反射機(jī)制原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了Java反射機(jī)制的相關(guān)概念、原理、基本使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2021-08-08
  • java awt生成簽名圖片如何消除鋸齒化

    java awt生成簽名圖片如何消除鋸齒化

    這篇文章主要介紹了java awt生成簽名圖片如何消除鋸齒化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Springboot如何通過(guò)自定義工具類獲取bean

    Springboot如何通過(guò)自定義工具類獲取bean

    這篇文章主要介紹了Springboot通過(guò)自定義工具類獲取bean方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 關(guān)于HashMap相同key累加value的問(wèn)題

    關(guān)于HashMap相同key累加value的問(wèn)題

    這篇文章主要介紹了關(guān)于HashMap相同key累加value的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java快速生成PDF文檔的實(shí)例代碼

    Java快速生成PDF文檔的實(shí)例代碼

    在如今數(shù)字化時(shí)代,越來(lái)越多的人使用PDF文檔進(jìn)行信息傳遞和共享,而使用Java生成PDF文檔也成為了一個(gè)非常重要的技能,所以本文我們將為您介紹如何使用Java快速生成PDF文檔,需要的朋友可以參考下
    2023-09-09
  • RabbitMQ  @RabbitListener 與 @RabbitHandler 的使用區(qū)別解析

    RabbitMQ  @RabbitListener 與 @RabbitHandl

    本文將深入探討這兩個(gè)注解的區(qū)別、使用方法、最佳實(shí)踐以及常見(jiàn)問(wèn)題,幫助開(kāi)發(fā)者更好地理解和應(yīng)用 RabbitMQ 在 Spring Boot 項(xiàng)目中的消息處理機(jī)制,感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • 一文帶你掌握J(rèn)ava?SPI的原理和實(shí)踐

    一文帶你掌握J(rèn)ava?SPI的原理和實(shí)踐

    在Java中,我們經(jīng)常會(huì)提到面向接口編程,這樣減少了模塊之間的耦合,更加靈活,Java?SPI?(Service?Provider?Interface)就提供了這樣的機(jī)制,本文就來(lái)講講它的原理與具體使用吧
    2023-05-05
  • Maven Repository倉(cāng)庫(kù)的具體使用

    Maven Repository倉(cāng)庫(kù)的具體使用

    本文主要介紹了Maven Repository倉(cāng)庫(kù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Spring Web MVC和Hibernate的集成配置詳解

    Spring Web MVC和Hibernate的集成配置詳解

    這篇文章主要介紹了Spring Web MVC和Hibernate的集成配置詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • 詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法

    詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法

    ByteArrayInputStream和ByteArrayOutputStream分別集成自InputStream和OutputStream這兩個(gè)輸入和輸出流,這里我們就來(lái)詳解Java中ByteArray字節(jié)數(shù)組的輸入輸出流的用法,需要的朋友可以參考下
    2016-06-06

最新評(píng)論

忻州市| 大石桥市| 安阳市| 普安县| 襄樊市| 大连市| 杂多县| 广宁县| 杭锦后旗| 汉沽区| 大余县| 响水县| 花莲县| 巴塘县| 平顺县| 樟树市| 峨眉山市| 甘谷县| 永吉县| 襄城县| 绥江县| 定南县| 白河县| 辽阳县| 博罗县| 沙坪坝区| 南汇区| 永城市| 孝昌县| 溆浦县| 雷波县| 昭平县| 小金县| 霍林郭勒市| 迁西县| 安国市| 黔西| 黎城县| 保亭| 永清县| 大埔区|