自定义一个 Spring Boot Starter -笔记

SpringBoot Starter的介绍参考: Spring Boot Starter简介-笔记-CSDN博客。这里介绍如何自定义一个springBoot Starter。

1. 项目结构

创建一个 Maven 项目,结构如下:

java 复制代码
custom-spring-boot-starter-demo/
├── custom-hello-jdk/  # jdk模块,包含功能逻辑
├── custom-hello-spring-boot-starter-jdk/  #Starter模块

2. 项目代码

2.1 custom-hello-jdk模块

step1. pom.xml

XML 复制代码
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>custom-hello-jdk</artifactId>
    <version>1.0.0</version>

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

step2. 定义功能接口和实现类

java 复制代码
package com.example.demo;

public interface HelloService {
    String sayHello();
}

//----------------------------------------------------

package com.example.demo;

import org.springframework.stereotype.Service;

@Service
public class DefaultHelloService implements HelloService {
    private final String message;

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

    @Override
    public String sayHello() {
        return message;
    }
}

step3. 配置属性类

java 复制代码
package com.example.demo;

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

@Data
@ConfigurationProperties(prefix = "custom.hello")
public class HelloProperties {
    private String message = "Hello from Custom Starter!";
}

step4. 自动配置类

java 复制代码
package com.example.demo;

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 {

    private final HelloProperties helloProperties;

    public HelloAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        return new DefaultHelloService(helloProperties.getMessage());
    }
}

2.2 custom-hello-spring-boot-starter-jdk模块

step1.pom.xml添加对custom-hello-jdk的依赖

XML 复制代码
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>custom-hello-spring-boot-starter-jdk</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <dependencies>
        <!-- 引入自动配置模块 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>custom-hello-jdk</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

step2.注册自动配置

src/main/resources/META-INF/spring.factories 中添加:

XML 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.demo.HelloAutoConfiguration

**备注:**自动配置注册也可以放到custom-hello-jdk模块里,在starter里仅放pom文件,作为依赖聚合,方便用户引入。

step3.构建和发布

  1. 执行 mvn clean install 将 Starter 安装到本地 Maven 仓库
  2. 或通过 mvn deploy 发布到远程仓库

2.3 使用自定义 Starter

背景:在另外一个项目中使用自定义的Starter。

step1. 在另一个 Spring Boot 项目中引入依赖

XML 复制代码
<dependency>
    <groupId>com.example</groupId>
    <artifactId>custom-hello-spring-boot-starter-jdk</artifactId>
    <version>1.0.0</version>
</dependency>

step2. 配置属性(可选)

XML 复制代码
custom.hello.message=Hello from Config!

step3.使用Bean

java 复制代码
import com.example.demo.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

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

2.4 总结

上述自定义springBoot Starter的完整流程说明:

  1. jdk功能模块:包含实际功能代码、配置属性等
  2. Starter 模块:作为依赖聚合,方便用户引入
  3. 条件化配置 :通过 @ConditionalOnMissingBean 避免重复 Bean
  4. 属性绑定 :使用 @ConfigurationProperties 实现灵活配置
相关推荐
ldj20204 分钟前
SpringBoot为什么使用new RuntimeException() 来获取调用栈?
java·spring boot·后端
超龄超能程序猿5 分钟前
Spring 应用中 Swagger 2.0 迁移 OpenAPI 3.0 详解:配置、注解与实践
java·spring boot·后端·spring·spring cloud
风象南17 分钟前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山18 分钟前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y40900118 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao19 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克16819 分钟前
java常见的jvm内存分析工具
java·jvm·数据库
笑衬人心。20 分钟前
TCP 拥塞控制算法 —— 慢启动(Slow Start)笔记
笔记·tcp/ip·php
花海如潮淹32 分钟前
前端性能追踪工具:用户体验的毫秒战争
前端·笔记·ux
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud