自定义spring-boot-starter

自定义命名规范:格式:{项目名}-spring-boot-starter

1. 项目结构

javascript 复制代码
my-spring-boot-starter
├── src
│   └── main
│       ├── java
│       │   └── com/example/starter
│       │       ├── MyStarterProperties.java
│       │       └── MyStarterAutoConfiguration.java
│       └── resources
│           └── META-INF
│               └── spring.factories
└── pom.xml

2. 添加依赖(pom.xml

javascript 复制代码
<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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

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

        <!-- 可选:使application.yml 有配置提示功能  optional=ture 只在当前项目有用,不会进行依赖传递 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

3.1 配置属性类(MyStarterProperties.java

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

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

@ConfigurationProperties(prefix = "my.starter")
public class MyStarterProperties {

    private String message = "默认消息";
    
    public String getMessage() { return message; }

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

3.2 自动配置类(MyStarterAutoConfiguration.java

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

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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(MyStarterProperties.class)
public class MyStarterAutoConfiguration {
    
    // MyServiceImpl 功能实现类
    @Bean
    @ConditionalOnMissingBean
    public MyService myService(MyStarterProperties properties) {
        return new MyServiceImpl(properties.getMessage());
    }
}

4. 配置自动加载(spring.factories

  • 方式 1(spring-boot 2.x) :在 src/main/resources/META-INF/spring.factories 中添加,如果有多个自动配置类,使用英文逗号分割
javascript 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.starter.MyStarterAutoConfiguration
  • 方式 2(Spring Boot 3.x+ 推荐) :在 src/main/resources/META-INF/spring/AutoConfiguration.imports 中直接写类名,如果有多个自动配置类,不需要使用任何分隔符(如逗号、分号、空格等) 。每个类名应该独占一行
javascript 复制代码
com.example.starter.MyStarterAutoConfiguration

5. 放入本地maven仓库

javascript 复制代码
mvn clean install

6. 其它项目(模块)引入使用

javascript 复制代码
<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
相关推荐
Pitayafruit12 分钟前
Spring AI 进阶之路04:集成 SearXNG 实现联网搜索
spring boot·后端·ai编程
在努力的前端小白7 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
白仑色15 小时前
Spring Boot 全局异常处理
java·spring boot·后端·全局异常处理·统一返回格式
Monly2115 小时前
RabbitMQ:SpringAMQP 入门案例
spring boot·rabbitmq·java-rabbitmq
Monly2116 小时前
RabbitMQ:SpringAMQP Fanout Exchange(扇型交换机)
spring boot·rabbitmq·java-rabbitmq
每天学习一丢丢16 小时前
Spring Boot + Vue 项目用宝塔面板部署指南
vue.js·spring boot·后端
杨DaB17 小时前
【SpringBoot】Dubbo、Zookeeper
spring boot·后端·zookeeper·dubbo·java-zookeeper
柯南二号17 小时前
【后端】SpringBoot中HttpServletRequest参数为啥不需要前端透传
前端·spring boot·后端
盖世英雄酱5813618 小时前
第一个RAG项目遇到的问题
java·spring boot
RainbowSea21 小时前
伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 06
java·spring boot·后端