自定义 Starter 实战

是的,自定义 Spring Boot Starter 需要基于 Spring Boot 项目,但它本身不需要依赖具体的业务项目,而是作为一个 独立的模块,被其他 Spring Boot 项目引用。

1. 为什么需要基于 Spring Boot?

Spring Boot Starter 依赖 Spring Boot 自动配置(Spring Boot AutoConfiguration) 机制,如果不是基于 Spring Boot:

  • 无法使用 @Configuration@ConditionalOnMissingBean 进行自动配置
  • 无法使用 spring.factories 让 Spring Boot 自动加载配置
  • 无法享受 Spring Boot 的简化依赖管理和默认配置

2. 自定义 Starter 的核心

自定义 Starter 通常包含 三个部分

  1. 自动配置类(使用 @Configuration
  2. META-INF/spring.factories 配置(让 Spring Boot 自动加载)
  3. 封装功能组件(例如 Service、Properties 等)

3. 自定义 Starter 实战

(1)创建一个 Starter 项目

新建一个 独立的 Maven 项目(不需要 Spring Boot 主程序),假设项目名为 my-spring-boot-starter

添加 pom.xml

<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>my-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <!-- 依赖 Spring Boot 自动配置 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

🔹 说明

  • spring-boot-autoconfigure 负责支持 Spring Boot 的自动配置机制
  • scope=provided 表示 Starter 自己不会包含 Spring Boot,而是由使用它的项目提供

(2)定义一个 Service

创建 com.example.mystarter.service.MyService

package com.example.mystarter.service;

public class MyService {
    public String sayHello() {
        return "Hello from My Starter!";
    }
}


(3)创建自动配置类

com.example.mystarter.config 目录下创建 MyStarterAutoConfiguration.java

package com.example.mystarter.config;

import com.example.mystarter.service.MyService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyStarterAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public MyService myService() {
        return new MyService();
    }
}

🔹 说明

  • @Configuration:标识为 Spring 配置类
  • @Bean:定义 MyService Bean
  • @ConditionalOnMissingBean:如果 Spring 容器中 没有 MyService,则自动创建(避免用户手动配置冲突)

(4)创建 spring.factories

resources/META-INF/ 目录下创建 spring.factories 文件:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.mystarter.config.MyStarterAutoConfiguration

🔹 说明

  • spring.factories 是 Spring Boot 识别 Starter 的关键
  • EnableAutoConfiguration 机制会自动加载 MyStarterAutoConfiguration

4. 在 Spring Boot 项目中使用

Starter 打包发布后,其他 Spring Boot 项目可以这样使用:

(1)引入 Starter

pom.xml 中添加:

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

(2)在 Spring Boot 项目中使用

import com.example.mystarter.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RequestMapping("/api")
public class MyApp {

    @Autowired
    private MyService myService;

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

    @GetMapping("/hello")
    public String hello() {
        return myService.sayHello();
    }
}

运行后访问 http://localhost:8080/api/hello,返回:

Hello from My Starter!


5. 总结

Spring Boot Starter 必须基于 Spring Boot 的自动配置机制,否则无法被 Spring Boot 项目识别和自动加载。
Starter 是一个独立的模块,不依赖具体业务项目,可以被多个 Spring Boot 项目复用。
核心步骤

  1. 创建 spring-boot-autoconfigure 依赖的项目
  2. 定义 @Configuration 自动配置类
  3. 使用 spring.factories 让 Spring Boot 发现 Starter
  4. 在 Spring Boot 项目中引入 Starter 并使用

👉 自定义 Starter 非常适合封装公共功能,提高代码复用性! 🚀

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注