手写一个简单的starter

2024-06-10 17:12
文章标签 简单 手写 starter

本文主要是介绍手写一个简单的starter,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

手写一个简单的starter

先了解一下什么是 starter:一个 starter其实就是对一个功能的集成封装,然后对外提供一个依赖,让业务去使

用,像我们熟悉的 Redis,mongo,mybatis 等均属于。

编写完starter后,可以提交到公司私有仓库供其他项目组进行调用。

1、命名规范

由于任何人都可以编写自己的 starter,为了区分官方的 starter 和个人的 starter,通常在命名上面会有一个规

范。SpringBoot 官方提出规范如下:

官方命名

作为前缀:spring-boot-starter-xxx

比如:spring-boot-starter-web…

(第三方)自定义命名

作为后缀:xxx-spring-boot-starter

比如:test-spring-boot-starter

自动装配首先要有一个配置类,其次还要有 spring.factories 文件,所以这两步是必不可少的。

2、创建流程规范

  • 定义核心业务类,这是该starter存在的意义

  • 完成自动配置类,目的是完成业务类的实例化

  • 若核心业务类中需要从配置文件获取配置数据,还需要定义一个用于封装配置文件中相关属性的类

  • 定义 META-INF/spring.factories 配置文件,用于对自动配置类进行注册。

3、starter代码编写

3.1 引入自动装配依赖

编写配置类首先要添加一个自动装配的依赖,然后再编写对应的配置类和业务实现类,在 pom 中添加如下依赖。

<?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"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.4.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>my-starter</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

3.2 编写配置类

我们要编写一个提供被springboot管理的自动配置的stater,首先我们要做的是编写配置类

package com.example.starter.config;import com.example.starter.pojo.ShareDemo;
import com.example.starter.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;@Configuration
@ConditionalOnClass(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties properties;@Bean@ConditionalOnProperty(name = "hello.service.enable", havingValue = "true")public HelloService worldService() {return new HelloService(properties.getName());}@Bean@ConditionalOnProperty(name = "hello.service.enable", havingValue = "false")public HelloService worldService1() {return new HelloService("z");}@Bean@ConditionalOnMissingBeanpublic HelloService worldService2() {return new HelloService("vvv");}@Beanpublic ShareDemo getShareDemo(HelloProperties worldProperties) {ShareDemo shareDemo = new ShareDemo();shareDemo.setName(worldProperties.getName());return shareDemo;}
}

3.3 属性类

package com.example.starter.config;import org.springframework.boot.context.properties.ConfigurationProperties;//属性前缀,要读取配置文件中的"hello.service.name"
@ConfigurationProperties("hello.service")
public class HelloProperties {private String name;public HelloProperties() {}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

3.4 业务实现类

package com.example.starter.service;public class HelloService {public HelloService(String name) {this.name = name;}private String name;public String info() {return "your info: " + name;}}

3.5 增加配置文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.starter.config.HelloServiceAutoConfiguration

4、使用

4.1 pom依赖

<?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 https://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.2.4.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>use-my-starter</artifactId><version>0.0.1-SNAPSHOT</version><name>use-my-starter</name><description>use-my-starter</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- 自己定义的starter --><dependency><groupId>org.example</groupId><artifactId>my-starter</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

4.2 application.properties配置文件

hello.service.name=welcome
hello.service.enable=true

4.3 测试

package com.example.usemystarter;import com.example.starter.service.HelloService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class UseMyStarterApplicationTests {@Autowiredprivate HelloService helloService;@Testvoid contextLoads() {// your info: welcomeSystem.out.println(helloService.info());}}

修改application.properties配置文件:

hello.service.name=welcome
hello.service.enable=false

结果为:your info: z

去掉application.properties配置文件中的内容,结果为:your info: vvv

这篇关于手写一个简单的starter的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048765

相关文章

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

利用Python编写一个简单的聊天机器人

《利用Python编写一个简单的聊天机器人》这篇文章主要为大家详细介绍了如何利用Python编写一个简单的聊天机器人,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 使用 python 编写一个简单的聊天机器人可以从最基础的逻辑开始,然后逐步加入更复杂的功能。这里我们将先实现一个简单的

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

使用PyQt5编写一个简单的取色器

《使用PyQt5编写一个简单的取色器》:本文主要介绍PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16进制颜色编码,一款跟随鼠标刷新图像的RGB和16... 目录取色器1取色器2PyQt5搭建的一个取色器,一共写了两款应用,一款使用快捷键捕获鼠标附近图像的RGB和16

四种简单方法 轻松进入电脑主板 BIOS 或 UEFI 固件设置

《四种简单方法轻松进入电脑主板BIOS或UEFI固件设置》设置BIOS/UEFI是计算机维护和管理中的一项重要任务,它允许用户配置计算机的启动选项、硬件设置和其他关键参数,该怎么进入呢?下面... 随着计算机技术的发展,大多数主流 PC 和笔记本已经从传统 BIOS 转向了 UEFI 固件。很多时候,我们也

基于Qt开发一个简单的OFD阅读器

《基于Qt开发一个简单的OFD阅读器》这篇文章主要为大家详细介绍了如何使用Qt框架开发一个功能强大且性能优异的OFD阅读器,文中的示例代码讲解详细,有需要的小伙伴可以参考一下... 目录摘要引言一、OFD文件格式解析二、文档结构解析三、页面渲染四、用户交互五、性能优化六、示例代码七、未来发展方向八、结论摘要

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b