spring boot 自定义starter示例

springboot 约定规范

Starter项目的命名规范

建议自定义的starter 以 xxx-spring-boot-starter 命名,官方的Starter一般都是以spring-boot-starter-为前缀。这样做的目的是为了避免与官方或其他第三方提供的Starter产生冲突或混淆。

Starter项目的结构规范(重要)

最核心的是 Spring Boot自动装配文件

作用: 用来指定我们的自动配置类,让Spring Boot能够在启动时自动扫描并加载它。

名称必须为 spring.factories

路径必须为resources/META-INF/spring.factories 这是springboot的约定规范,不遵守一律out。

hello-spring-boot-starter

pom.xml

依赖说明

  1. spring-boot-configuration-processor : 编译时依赖 可以帮助我们生成属性类和配置元数据,并且设置为可选依赖,避免传递给其他项目。
  2. spring-boot-starter : 基础依赖 提供了Spring Boot核心功能和默认配置
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>org.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!--        jdk版本-->
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 基础依赖  提供了Spring Boot核心功能和默认配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.7.18</version>
        </dependency>
        <!-- 编译时依赖 可以帮助我们生成属性类和配置元数据,并且设置为可选依赖,避免传递给其他项目。-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.7.18</version>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>

Starter项目的属性类

在创建一个自定义的Starter项目时,我们需要编写一个属性类,用来定义我们要集成的功能模块所需的配置项,并且使用@ConfigurationProperties注解来指定用户配置文件中的前缀。

java 复制代码
package com.hello.starter.properties;

/**
 *  * 描述:配置信息 实体
 * @author yss
 * @date 2024/5/1
 */
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "demo") // 指定 用户配置文件中的前缀
public class DemoProperties {
    private String sayWhat;
    private String toWho;

    public String getSayWhat() {
        return sayWhat;
    }

    public void setSayWhat(String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
        return toWho;
    }

    public void setToWho(String toWho) {
        this.toWho = toWho;
    }
}

Starter项目的业务功能类

在创建一个自定义的Starter项目时,我们需要编写一个或多个业务功能类,用来实现我们要集成的功能模块的具体逻辑。

java 复制代码
package com.hello.starter.service;

/**
 * @author yss
 * @date 2024/5/1
 */
public class DemoService {
    public String sayWhat;
    public String toWho;
    public DemoService(String sayWhat, String toWho){
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }
    public String say(){
        return this.sayWhat + "!  " + toWho;
    }
}

Starter项目的自动配置类(重要)

在创建一个自定义的Starter项目时,我们需要编写一个自动配置类,用来根据属性类和业务功能类,创建相应的Bean对象。Springboot自动配置原理源码解读

  1. @EnableConfigurationProperties : 启用属性类,并将其注入到配置类中。
  2. @ConditionalOnProperty: 判断用户配置文件中是否有相应的配置项,存在并且符合期望则满足条件
  3. @Configuration : 标识这是一个配置类,用来创建和注册Bean对象。
  4. @Bean: 根据属性类和业务功能类,创建相应类型的Bean对象,并注册到应用上下文中。
  5. @ConditionalOnClass: 判断业务功能类是否存在
java 复制代码
package com.hello.starter.config;

import com.hello.starter.properties.DemoProperties;
import com.hello.starter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @author yss
 * @date 2024/5/1
 */
@Configuration // 标识这是一个配置类,用来创建和注册Bean对象。
//启用属性类,并将其注入到配置类中。
@EnableConfigurationProperties(DemoProperties.class) 
// 判断业务功能类是否存在
@ConditionalOnClass(DemoService.class)
// 判断用户配置文件中是否存在指定的属性(isopen),
// 如果存在并且值与期望相符, 则满足条件(开启相应功能)。
@ConditionalOnProperty(
        prefix = "demo",
        name = "isopen",
        havingValue = "true"
)  
public class DemoConfig {
    @Autowired
    private DemoProperties demoProperties;
    
	// 根据属性类和业务功能类,创建相应类型的Bean对象,并注册到应用上下文中。
    @Bean(name = "demo")  
    public DemoService demoService(){
        return new DemoService(demoProperties.getSayWhat(), 
        demoProperties.getToWho());
    }
}

Starter项目的自动装配文件(重要)

在resources/META-INF目录下创建一个名为spring.factories的文件,用来指定我们的自动配置类,让Spring Boot能够在启动时自动扫描并加载它。以下是一个示例:

xml 复制代码
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hello.starter.config.DemoConfig

打包发布

执行 mvn clean install 命令 一个自定义的starter就完成了。

做完上面这几步,我们自定义Starter就完成了,下面我们来测试一下

引入依赖

在我们需要的项目中引入依赖。

xml 复制代码
<!--        自定义Starter-->
<dependency>
    <groupId>org.example</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

application.yml

yml 复制代码
demo:
  # 允许 demo作为bean注入IOC容器  一定要指明 isopen  不然项目无法启动
  isopen: true
  say-what: hello
  to-who: shf

DemoController.java

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

import com.hello.starter.service.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yss
 * @date 2024/5/1
 */
@RestController
public class DemoController {

    @Resource(name = "demo")
    private DemoService demoService;

    @RequestMapping("/say")
    public String sayWhat(){
        return demoService.say();
    }
}

启动项目并且运行

ok了!!

自定义starter简要步骤
优雅地自定义Starter注解配置说明

相关推荐
特立独行的猫A2 小时前
用仓颉语言 cj-tauri 框架,五分钟给娃做了个鸿蒙形近字游戏
java·后端·python
码事漫谈5 小时前
我花了一周时间,看AI公司怎么"管"AI,结果越看越后背发凉
后端
爱学习的程序媛8 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
GISer_Jing8 小时前
Agent核心内容
后端·ai·langchain
budingxiaomoli9 小时前
Spring统一功能处理
java·后端·spring
某鹏10 小时前
网关选型:Go、Java、Graal 谁都不能完胜
后端
用户02478365309710 小时前
从屎山到防线:14 行状态迁移表,给任务系统装上防瞎跳闸门
后端
CQU_JIAKE10 小时前
11.13[体系结构】指标,五级流水线,八级流水线,虚实地址思考
后端
Flynn Liang11 小时前
可观测性与成本治理:一次对话的 17 个观测点
java·langchain·可观测性·micrometer·langchain4j·token 成本·llm 监控
金銀銅鐵11 小时前
带您看懂字节码指令(三):同一行加法,为什么有四种指令
java·jvm