SpringBoot中自定义Starter的完整教程

 更新时间:2025年11月14日 10:22:54   作者:杰西笔记  
在 Spring Boot 中,自定义 Starter 是一种非常强大的方式,可以将常用的功能模块封装成可复用的组件,下面我们就来看看具体的实现方法吧

前言

在 Spring Boot 中,自定义 Starter 是一种非常强大的方式,可以将常用的功能模块封装成可复用的组件。本文将详细介绍如何创建一个自定义 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:配置自动配置类的入口文件

二、项目结构设计

我们以一个简单的“Hello World”功能为例,创建一个名为 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
 

三、代码实现

1. 创建 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(业务逻辑类)

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. 创建 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 只是一个“门面”,真正实现自动配置的是 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!
    }
}

五、完整流程总结

步骤描述
1创建 autoconfigure 模块,包含自动配置逻辑
2编写 @Configuration 类,使用 @EnableConfigurationProperties 绑定配置
3在 META-INF/spring.factories 中注册自动配置类
4创建 starter 模块,仅依赖 autoconfigure
5在主项目中引入 starter,无需额外配置
6通过 application.yml 设置参数,自动生效

六、关键注解说明

注解作用
@Configuration标记为配置类
@EnableConfigurationProperties启用配置属性绑定
@ConditionalOnMissingBean当容器中没有该 Bean 时才创建
@Component将类注册为 Spring Bean
@ConfigurationProperties绑定配置文件前缀属性

七、扩展建议

  • 支持多环境配置(如 dev, prod
  • 添加日志记录或监控功能
  • 使用 @ConditionalOnClass@ConditionalOnProperty 实现更复杂的条件判断
  • 提供接口供外部扩展

八、注意事项

  • spring.factories 文件格式必须正确,不能有空格或换行错误。
  • 确保 autoconfigure 模块的 spring-boot-autoconfigure 依赖已添加。
  • 启动器模块不要直接写业务逻辑,只做依赖管理。
  • 版本号保持一致,避免冲突。

九、运行效果

启动项目后,控制台输出

Hello, Spring Boot!

说明自定义 Starter 成功加载并执行了自动配置逻辑。

总结:通过这种方式,你可以轻松地将任何功能封装为可复用的 Spring Boot Starter,提升开发效率与代码复用性。

到此这篇关于SpringBoot中自定义Starter的完整教程的文章就介绍到这了,更多相关SpringBoot自定义Starter内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java单机环境实现定时任务技术

    Java单机环境实现定时任务技术

    这篇文章主要介绍了Java单机环境实现定时任务技术,文章内容介绍详细,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • IntelliJ IDEA maven 构建简单springmvc项目(图文教程)

    IntelliJ IDEA maven 构建简单springmvc项目(图文教程)

    在工作当中,我们有时需要创建一个全新的工程,而基于spring-mvc web的工程较为常见,这篇文章主要介绍了IntelliJ IDEA maven 构建简单springmvc项目(图文教程),感兴趣的小伙伴们可以参考一下
    2018-05-05
  • springboot自定义stater启动流程

    springboot自定义stater启动流程

    这篇文章主要介绍了springboot自定义stater启动流程,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • java中的文件操作总结(干货)

    java中的文件操作总结(干货)

    本篇文章主要介绍了java中的文件操作总结(干货),主要有文件读写,遍历文件夹,文件夹操作等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-02-02
  • 最长重复子数组 findLength示例详解

    最长重复子数组 findLength示例详解

    今天给大家分享一道比较常问的算法面试题,最长重复子数组 findLength,文中给大家分享解题思路,结合示例代码介绍的非常详细,需要的朋友参考下吧
    2023-08-08
  • java实现收藏功能

    java实现收藏功能

    这篇文章主要为大家详细介绍了java实现收藏功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • java 线程公平锁与非公平锁详解及实例代码

    java 线程公平锁与非公平锁详解及实例代码

    这篇文章主要介绍了java 线程公平锁与非公平锁详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Idea jdk版本问题解决方案

    Idea jdk版本问题解决方案

    这篇文章主要介绍了Idea jdk版本问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringMVC Controller解析ajax参数过程详解

    SpringMVC Controller解析ajax参数过程详解

    这篇文章主要介绍了SpringMVC Controller解析ajax参数过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • Java项目在Idea中开发遇到所有代码爆红的问题与解决办法

    Java项目在Idea中开发遇到所有代码爆红的问题与解决办法

    今天打开项目时发现idea竟然爆红,通过查找相关资料用于解决,下面这篇文章主要给大家介绍了关于Java项目在Idea中开发遇到所有代码爆红的问题与解决办法的相关资料,需要的朋友可以参考下
    2023-06-06

最新评论