spring boot 自定义starter示例

embedded/2024/9/23 10:47:26/

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 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 {@Autowiredprivate DemoProperties demoProperties;// 根据属性类和业务功能类,创建相应类型的Bean对象,并注册到应用上下文中。@Bean(name = "demo")  public DemoService demoService(){return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho());}
}

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

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

#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hello.starter.config.DemoConfig

打包发布

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

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

引入依赖

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

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

在这里插入图片描述

application.yml

demo:# 允许 demo作为bean注入IOC容器  一定要指明 isopen  不然项目无法启动isopen: truesay-what: helloto-who: shf

java_201">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注解配置说明


http://www.ppmy.cn/embedded/30271.html

相关文章

人工智能论文:GPT, GPT-2, GPT-3 对比和演进的思路

2018.6 GPT&#xff1a; Improving Language Understanding by Generative Pre-Training 第一篇主要强调 无监督预训练有监督微调transformer 主要成果&#xff1a; 1&#xff0c;无监督预训练&#xff1a;使得模型能够从海量未标记数据中自主学习&#xff0c;为后续任务提供了…

SSM+Vue在线OA办公系统

在线办公分三个用户登录&#xff0c;管理员&#xff0c;经理&#xff0c;员工。 SSM架构&#xff0c;maven管理工具&#xff0c;数据库Mysql&#xff0c;系统有文档&#xff0c;可有偿安装调试及讲解&#xff0c;项目保证质量。需要划到 最底 下可以联系到我。 功能如下&am…

实习面试之算法准备:数学题

目录 1 技巧2 例题2.1 Nim 游戏2.2 石子游戏2.3 灯泡开关 1 技巧 稍加思考&#xff0c;找到规律 2 例题 2.1 Nim 游戏 你和你的朋友&#xff0c;两个人一起玩 Nim 游戏&#xff1a; 桌子上有一堆石头。 你们轮流进行自己的回合&#xff0c; 你作为先手 。 每一回合&#xf…

B树:原理、操作及应用

B树&#xff1a;原理、操作及应用 一、引言二、B树概述1. 定义与性质2. B树与磁盘I/O 三、B树的基本操作1. 搜索&#xff08;B-TREE-SEARCH&#xff09;2. 插入&#xff08;B-TREE-INSERT&#xff09;3. 删除&#xff08;B-TREE-DELETE&#xff09; 四、B树的C代码实现示例五、…

【设计模式】抽象工厂模式(Abstract Factory Pattern)

目录标题 抽象工厂设计模式详解1. 介绍2. 结构3. 实现步骤3.1 创建抽象产品接口3.2 创建具体产品类3.3 创建抽象工厂接口3.4 创建具体工厂类 4. 好处与优点5. 坏处与缺点6. 适用场景7. 总结 抽象工厂设计模式详解 1. 介绍 抽象工厂模式是一种创建型设计模式&#xff0c;它提供…

【中断】【ARM64】学习总结

optee中的异常向量表解读–中断处理解读 https://mp.weixin.qq.com/s/gBsy4YDYTHGRsy2zcVr6Vg

Apache DolphinScheduler支持Flink吗?

随着大数据技术的快速发展&#xff0c;很多企业开始将Flink引入到生产环境中&#xff0c;以满足日益复杂的数据处理需求。而作为一款企业级的数据调度平台&#xff0c;Apache DolphinScheduler也跟上了时代步伐&#xff0c;推出了对Flink任务类型的支持。 Flink是一个开源的分…

笨蛋学C++【C++基础第九弹】

C基础第八弹 5.C模板函数模板类模板 6.C预处理器#define 预处理参数宏条件编译# 和 ## 运算符C 中的预定义宏 7.C信号处理signal() 函数raise() 函数 5.C模板 模板是泛型编程的基础&#xff0c;泛型编程即以一种独立于任何特定类型的方式编写代码 函数模板 语法&#xff1a; …